-
Notifications
You must be signed in to change notification settings - Fork 327
Expand file tree
/
Copy pathJsonResponse.php
More file actions
146 lines (133 loc) Β· 3.12 KB
/
Copy pathJsonResponse.php
File metadata and controls
146 lines (133 loc) Β· 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\Http;
use JsonSerializable;
use OCA\Mail\Exception\ClientException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse as Base;
use Throwable;
use function array_flip;
use function array_intersect_key;
use function array_map;
use function array_merge;
use function get_class;
/**
* @see https://github.com/omniti-labs/jsend
* @psalm-suppress MissingTemplateParam
* @todo spec template with 28+
*/
class JsonResponse extends Base {
/**
* @param Http::STATUS_* $statusCode
*/
public function __construct($data = [],
int $statusCode = Http::STATUS_OK) {
parent::__construct($data, $statusCode);
$this->addHeader('x-mail-response', 'true');
}
/**
* @param array|JsonSerializable|bool|string $data
* @param Http::STATUS_* $status
*
* @return static
*/
public static function success($data = null,
int $status = Http::STATUS_OK): self {
return new self(
[
'status' => 'success',
'data' => $data,
],
$status
);
}
/**
* @param array|JsonSerializable|bool|string $data
* @param Http::STATUS_* $status
*
* @return static
*/
public static function fail($data = null,
int $status = Http::STATUS_BAD_REQUEST): self {
return new self(
[
'status' => 'fail',
'data' => $data,
],
$status
);
}
public static function failWith(ClientException $exception): self {
return self::fail(
[
'message' => $exception->getMessage(),
'type' => get_class($exception),
],
$exception->getHttpCode()
);
}
/**
* @param string $message
* @param Http::STATUS_* $status
* @param array|JsonSerializable|bool|string $data
*
* @return static
*/
public static function error(string $message,
int $status = Http::STATUS_INTERNAL_SERVER_ERROR,
$data = [],
int $code = 0): self {
return new self(
[
'status' => 'error',
'message' => $message,
'data' => $data,
'code' => $code,
],
$status
);
}
/**
* @param Throwable $error
* @param Http::STATUS_* $status
* @param array|JsonSerializable|bool|string $data
*
* @return self
*/
public static function errorFromThrowable(Throwable $error,
int $status = Http::STATUS_INTERNAL_SERVER_ERROR,
$data = []): self {
if (!is_array($data)) {
$data = [$data];
}
return self::error(
$error->getMessage(),
$status,
array_merge(
$data,
self::serializeException($error)
),
$error->getCode()
);
}
private static function serializeException(?Throwable $throwable): ?array {
if ($throwable === null) {
return null;
}
return [
'type' => get_class($throwable),
'message' => $throwable->getMessage(),
'code' => $throwable->getCode(),
'trace' => self::filterTrace($throwable->getTrace()),
'previous' => self::serializeException($throwable->getPrevious()),
];
}
private static function filterTrace(array $original): array {
return array_map(static fn (array $row) => array_intersect_key($row,
array_flip(['file', 'line', 'function', 'class'])), $original);
}
}