@tryghost/errors 1.2.26 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,86 @@
1
+ import { GhostError, GhostErrorOptions } from './GhostError';
2
+ export declare class InternalServerError extends GhostError {
3
+ constructor(options?: GhostErrorOptions);
4
+ }
5
+ export declare class IncorrectUsageError extends GhostError {
6
+ constructor(options?: GhostErrorOptions);
7
+ }
8
+ export declare class NotFoundError extends GhostError {
9
+ constructor(options?: GhostErrorOptions);
10
+ }
11
+ export declare class BadRequestError extends GhostError {
12
+ constructor(options?: GhostErrorOptions);
13
+ }
14
+ export declare class UnauthorizedError extends GhostError {
15
+ constructor(options?: GhostErrorOptions);
16
+ }
17
+ export declare class NoPermissionError extends GhostError {
18
+ constructor(options?: GhostErrorOptions);
19
+ }
20
+ export declare class ValidationError extends GhostError {
21
+ constructor(options?: GhostErrorOptions);
22
+ }
23
+ export declare class UnsupportedMediaTypeError extends GhostError {
24
+ constructor(options?: GhostErrorOptions);
25
+ }
26
+ export declare class TooManyRequestsError extends GhostError {
27
+ constructor(options?: GhostErrorOptions);
28
+ }
29
+ export declare class MaintenanceError extends GhostError {
30
+ constructor(options?: GhostErrorOptions);
31
+ }
32
+ export declare class MethodNotAllowedError extends GhostError {
33
+ constructor(options?: GhostErrorOptions);
34
+ }
35
+ export declare class RequestNotAcceptableError extends GhostError {
36
+ constructor(options?: GhostErrorOptions);
37
+ }
38
+ export declare class RequestEntityTooLargeError extends GhostError {
39
+ constructor(options?: GhostErrorOptions);
40
+ }
41
+ export declare class TokenRevocationError extends GhostError {
42
+ constructor(options?: GhostErrorOptions);
43
+ }
44
+ export declare class VersionMismatchError extends GhostError {
45
+ constructor(options?: GhostErrorOptions);
46
+ }
47
+ export declare class DataExportError extends GhostError {
48
+ constructor(options?: GhostErrorOptions);
49
+ }
50
+ export declare class DataImportError extends GhostError {
51
+ constructor(options?: GhostErrorOptions);
52
+ }
53
+ export declare class EmailError extends GhostError {
54
+ constructor(options?: GhostErrorOptions);
55
+ }
56
+ export declare class ThemeValidationError extends GhostError {
57
+ constructor(options?: GhostErrorOptions);
58
+ }
59
+ export declare class DisabledFeatureError extends GhostError {
60
+ constructor(options?: GhostErrorOptions);
61
+ }
62
+ export declare class UpdateCollisionError extends GhostError {
63
+ constructor(options?: GhostErrorOptions);
64
+ }
65
+ export declare class HostLimitError extends GhostError {
66
+ constructor(options?: GhostErrorOptions);
67
+ }
68
+ export declare class HelperWarning extends GhostError {
69
+ constructor(options?: GhostErrorOptions);
70
+ }
71
+ export declare class PasswordResetRequiredError extends GhostError {
72
+ constructor(options?: GhostErrorOptions);
73
+ }
74
+ export declare class UnhandledJobError extends GhostError {
75
+ constructor(options?: GhostErrorOptions);
76
+ }
77
+ export declare class NoContentError extends GhostError {
78
+ constructor(options?: GhostErrorOptions);
79
+ }
80
+ export declare class ConflictError extends GhostError {
81
+ constructor(options?: GhostErrorOptions);
82
+ }
83
+ export declare class MigrationError extends GhostError {
84
+ constructor(options?: GhostErrorOptions);
85
+ }
86
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,iBAAiB,EAAC,MAAM,cAAc,CAAC;AAe3D,qBAAa,mBAAoB,SAAQ,UAAU;gBACnC,OAAO,GAAE,iBAAsB;CAQ9C;AAED,qBAAa,mBAAoB,SAAQ,UAAU;gBACnC,OAAO,GAAE,iBAAsB;CAQ9C;AAED,qBAAa,aAAc,SAAQ,UAAU;gBAC7B,OAAO,GAAE,iBAAsB;CAQ9C;AAED,qBAAa,eAAgB,SAAQ,UAAU;gBAC/B,OAAO,GAAE,iBAAsB;CAO9C;AAED,qBAAa,iBAAkB,SAAQ,UAAU;gBACjC,OAAO,GAAE,iBAAsB;CAO9C;AAED,qBAAa,iBAAkB,SAAQ,UAAU;gBACj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gBAC1C,OAAO,GAAE,iBAAsB;CAO9C;AAED,qBAAa,iBAAkB,SAAQ,UAAU;gBACjC,OAAO,GAAE,iBAAsB;CAO9C;AAED,qBAAa,cAAe,SAAQ,UAAU;gBAC9B,OAAO,GAAE,iBAAsB;CAO9C;AAED,qBAAa,aAAc,SAAQ,UAAU;gBAC7B,OAAO,GAAE,iBAAsB;CAO9C;AAED,qBAAa,cAAe,SAAQ,UAAU;gBAC9B,OAAO,GAAE,iBAAsB;CAO9C"}
@@ -0,0 +1,13 @@
1
+ import { GhostError } from './GhostError';
2
+ import * as ghostErrors from './errors';
3
+ import { deserialize, isGhostError, prepareStackForUser, serialize } from './utils';
4
+ export * from './errors';
5
+ export type { GhostError };
6
+ export default ghostErrors;
7
+ export declare const utils: {
8
+ serialize: typeof serialize;
9
+ deserialize: typeof deserialize;
10
+ isGhostError: typeof isGhostError;
11
+ prepareStackForUser: typeof prepareStackForUser;
12
+ };
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,cAAc,CAAC;AACxC,OAAO,KAAK,WAAW,MAAM,UAAU,CAAC;AACxC,OAAO,EAAC,WAAW,EAAE,YAAY,EAAE,mBAAmB,EAAE,SAAS,EAAC,MAAM,SAAS,CAAC;AAElF,cAAc,UAAU,CAAC;AACzB,YAAY,EAAC,UAAU,EAAC,CAAC;AACzB,eAAe,WAAW,CAAC;AAE3B,eAAO,MAAM,KAAK;;;;;CAKjB,CAAC"}
@@ -0,0 +1,33 @@
1
+ import { GhostError } from './GhostError';
2
+ type AnyObject = Record<string, any>;
3
+ /**
4
+ * @description Serialize GhostError instance to error JSON format
5
+ *
6
+ * jsonapi.org error format:
7
+ *
8
+ * source: {
9
+ * parameter: URL query parameter (no support yet)
10
+ * pointer: HTTP body attribute
11
+ * }
12
+ *
13
+ * @see http://jsonapi.org/format/#errors
14
+ */
15
+ export declare function serialize(err: GhostError, options?: {
16
+ format: 'jsonapi' | 'oauth';
17
+ }): AnyObject;
18
+ /**
19
+ * @description Deserialize from error JSON format to GhostError instance
20
+ */
21
+ export declare function deserialize(errorFormat: AnyObject): AnyObject;
22
+ /**
23
+ * @description Replace the stack with a user-facing one
24
+ * @returns Clone of the original error with a user-facing stack
25
+ */
26
+ export declare function prepareStackForUser(error: GhostError): GhostError;
27
+ export declare function prepareStackForUser(error: Error): Error;
28
+ /**
29
+ * @description Check whether an error instance is a GhostError.
30
+ */
31
+ export declare function isGhostError(err: Error): boolean;
32
+ export {};
33
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,UAAU,EAAC,MAAM,cAAc,CAAC;AAIxC,KAAK,SAAS,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AA+HpC;;;;;;;;;;;GAWG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE;IAAC,MAAM,EAAE,SAAS,GAAG,OAAO,CAAA;CAAC,aAiBjF;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,WAAW,EAAE,SAAS,GAAG,SAAS,CAU7D;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAAA;AAClE,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAAA;AAiCxD;;GAEG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,KAAK,WAkBtC"}
@@ -0,0 +1,2 @@
1
+ export declare function wrapStack(err: Error, internalErr: Error): string;
2
+ //# sourceMappingURL=wrap-stack.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"wrap-stack.d.ts","sourceRoot":"","sources":["../src/wrap-stack.ts"],"names":[],"mappings":"AAAA,wBAAgB,SAAS,CAAC,GAAG,EAAE,KAAK,EAAE,WAAW,EAAE,KAAK,UAIvD"}
package/index.js DELETED
@@ -1 +0,0 @@
1
- module.exports = require('./lib/errors');
package/lib/errors.js DELETED
@@ -1,340 +0,0 @@
1
- const uuid = require('uuid');
2
- const merge = require('lodash/merge');
3
- const isString = require('lodash/isString');
4
- const utils = require('./utils');
5
-
6
- class GhostError extends Error {
7
- constructor(options = {}) {
8
- super();
9
-
10
- /**
11
- * defaults
12
- */
13
- this.statusCode = 500;
14
- this.errorType = 'InternalServerError';
15
- this.level = 'normal';
16
- this.message = 'The server has encountered an error.';
17
- this.id = uuid.v1();
18
-
19
- /**
20
- * custom overrides
21
- */
22
- this.id = options.id || this.id;
23
- this.statusCode = options.statusCode || this.statusCode;
24
- this.level = options.level || this.level;
25
- this.context = options.context || this.context;
26
- this.help = options.help || this.help;
27
- this.errorType = this.name = options.errorType || this.errorType;
28
- this.errorDetails = options.errorDetails;
29
- // @ts-ignore
30
- this.code = options.code || null;
31
- this.property = options.property || null;
32
- this.redirect = options.redirect || null;
33
-
34
- this.message = options.message || this.message;
35
- this.hideStack = options.hideStack || false;
36
-
37
- // NOTE: Error to inherit from, override!
38
- // Nested objects are getting copied over in one piece (can be changed, but not needed right now)
39
- if (options.err) {
40
- // CASE: Support err as string (it happens that third party libs return a string instead of an error instance)
41
- if (isString(options.err)) {
42
- /* eslint-disable no-restricted-syntax */
43
- options.err = new Error(options.err);
44
- /* eslint-enable no-restricted-syntax */
45
- }
46
-
47
- Object.getOwnPropertyNames(options.err).forEach((property) => {
48
- if (['errorType', 'name', 'statusCode', 'message', 'level'].indexOf(property) !== -1) {
49
- return;
50
- }
51
-
52
- // CASE: `code` should put options as priority over err
53
- if (property === 'code') {
54
- // @ts-ignore
55
- this[property] = this[property] || options.err[property];
56
- return;
57
- }
58
-
59
- if (property === 'stack' && !this.hideStack) {
60
- this[property] = utils.wrapStack(this, options.err);
61
- return;
62
- }
63
-
64
- this[property] = options.err[property] || this[property];
65
- });
66
- }
67
- }
68
- }
69
-
70
- const ghostErrors = {
71
- InternalServerError: class InternalServerError extends GhostError {
72
- constructor(options) {
73
- super(merge({
74
- statusCode: 500,
75
- level: 'critical',
76
- errorType: 'InternalServerError',
77
- message: 'The server has encountered an error.'
78
- }, options));
79
- }
80
- },
81
- IncorrectUsageError: class IncorrectUsageError extends GhostError {
82
- constructor(options) {
83
- super(merge({
84
- statusCode: 400,
85
- level: 'critical',
86
- errorType: 'IncorrectUsageError',
87
- message: 'We detected a misuse. Please read the stack trace.'
88
- }, options));
89
- }
90
- },
91
- NotFoundError: class NotFoundError extends GhostError {
92
- constructor(options) {
93
- super(merge({
94
- statusCode: 404,
95
- errorType: 'NotFoundError',
96
- message: 'Resource could not be found.',
97
- hideStack: true
98
- }, options));
99
- }
100
- },
101
- BadRequestError: class BadRequestError extends GhostError {
102
- constructor(options) {
103
- super(merge({
104
- statusCode: 400,
105
- errorType: 'BadRequestError',
106
- message: 'The request could not be understood.'
107
- }, options));
108
- }
109
- },
110
- UnauthorizedError: class UnauthorizedError extends GhostError {
111
- constructor(options) {
112
- super(merge({
113
- statusCode: 401,
114
- errorType: 'UnauthorizedError',
115
- message: 'You are not authorised to make this request.'
116
- }, options));
117
- }
118
- },
119
- NoPermissionError: class NoPermissionError extends GhostError {
120
- constructor(options) {
121
- super(merge({
122
- statusCode: 403,
123
- errorType: 'NoPermissionError',
124
- message: 'You do not have permission to perform this request.'
125
- }, options));
126
- }
127
- },
128
- ValidationError: class ValidationError extends GhostError {
129
- constructor(options) {
130
- super(merge({
131
- statusCode: 422,
132
- errorType: 'ValidationError',
133
- message: 'The request failed validation.'
134
- }, options));
135
- }
136
- },
137
- UnsupportedMediaTypeError: class UnsupportedMediaTypeError extends GhostError {
138
- constructor(options) {
139
- super(merge({
140
- statusCode: 415,
141
- errorType: 'UnsupportedMediaTypeError',
142
- message: 'The media in the request is not supported by the server.'
143
- }, options));
144
- }
145
- },
146
- TooManyRequestsError: class TooManyRequestsError extends GhostError {
147
- constructor(options) {
148
- super(merge({
149
- statusCode: 429,
150
- errorType: 'TooManyRequestsError',
151
- message: 'Server has received too many similar requests in a short space of time.'
152
- }, options));
153
- }
154
- },
155
- MaintenanceError: class MaintenanceError extends GhostError {
156
- constructor(options) {
157
- super(merge({
158
- statusCode: 503,
159
- errorType: 'MaintenanceError',
160
- message: 'The server is temporarily down for maintenance.'
161
- }, options));
162
- }
163
- },
164
- MethodNotAllowedError: class MethodNotAllowedError extends GhostError {
165
- constructor(options) {
166
- super(merge({
167
- statusCode: 405,
168
- errorType: 'MethodNotAllowedError',
169
- message: 'Method not allowed for resource.'
170
- }, options));
171
- }
172
- },
173
- RequestNotAcceptableError: class RequestNotAcceptableError extends GhostError {
174
- constructor(options) {
175
- super(merge({
176
- statusCode: 406,
177
- errorType: 'RequestNotAcceptableError',
178
- message: 'Request not acceptable for provided Accept-Version header.',
179
- hideStack: true
180
- }, options));
181
- }
182
- },
183
- RequestEntityTooLargeError: class RequestEntityTooLargeError extends GhostError {
184
- constructor(options) {
185
- super(merge({
186
- statusCode: 413,
187
- errorType: 'RequestEntityTooLargeError',
188
- message: 'Request was too big for the server to handle.'
189
- }, options));
190
- }
191
- },
192
- TokenRevocationError: class TokenRevocationError extends GhostError {
193
- constructor(options) {
194
- super(merge({
195
- statusCode: 503,
196
- errorType: 'TokenRevocationError',
197
- message: 'Token is no longer available.'
198
- }, options));
199
- }
200
- },
201
- VersionMismatchError: class VersionMismatchError extends GhostError {
202
- constructor(options) {
203
- super(merge({
204
- statusCode: 400,
205
- errorType: 'VersionMismatchError',
206
- message: 'Requested version does not match server version.'
207
- }, options));
208
- }
209
- },
210
- DataExportError: class DataExportError extends GhostError {
211
- constructor(options) {
212
- super(merge({
213
- statusCode: 500,
214
- errorType: 'DataExportError',
215
- message: 'The server encountered an error whilst exporting data.'
216
- }, options));
217
- }
218
- },
219
- DataImportError: class DataImportError extends GhostError {
220
- constructor(options) {
221
- super(merge({
222
- statusCode: 500,
223
- errorType: 'DataImportError',
224
- message: 'The server encountered an error whilst importing data.'
225
- }, options));
226
- }
227
- },
228
- EmailError: class EmailError extends GhostError {
229
- constructor(options) {
230
- super(merge({
231
- statusCode: 500,
232
- errorType: 'EmailError',
233
- message: 'The server encountered an error whilst sending email.'
234
- }, options));
235
- }
236
- },
237
- ThemeValidationError: class ThemeValidationError extends GhostError {
238
- constructor(options) {
239
- super(merge({
240
- statusCode: 422,
241
- errorType: 'ThemeValidationError',
242
- message: 'The theme has a validation error.',
243
- errorDetails: {}
244
- }, options));
245
- }
246
- },
247
- DisabledFeatureError: class DisabledFeatureError extends GhostError {
248
- constructor(options) {
249
- super(merge({
250
- statusCode: 409,
251
- errorType: 'DisabledFeatureError',
252
- message: 'Unable to complete the request, this feature is disabled.'
253
- }, options));
254
- }
255
- },
256
- UpdateCollisionError: class UpdateCollisionError extends GhostError {
257
- constructor(options) {
258
- super(merge({
259
- statusCode: 409,
260
- errorType: 'UpdateCollisionError',
261
- message: 'Unable to complete the request, there was a conflict.'
262
- }, options));
263
- }
264
- },
265
- HostLimitError: class HostLimitError extends GhostError {
266
- constructor(options) {
267
- super(merge({
268
- errorType: 'HostLimitError',
269
- hideStack: true,
270
- statusCode: 403,
271
- message: 'Unable to complete the request, this resource is limited.'
272
- }, options));
273
- }
274
- },
275
- HelperWarning: class HelperWarning extends GhostError {
276
- constructor(options) {
277
- super(merge({
278
- errorType: 'HelperWarning',
279
- hideStack: true,
280
- statusCode: 400,
281
- message: 'A theme helper has done something unexpected.'
282
- }, options));
283
- }
284
- },
285
- PasswordResetRequiredError: class PasswordResetRequiredError extends GhostError {
286
- constructor(options) {
287
- super(merge({
288
- errorType: 'PasswordResetRequiredError',
289
- statusCode: 401,
290
- message: 'For security, you need to create a new password. An email has been sent to you with instructions!'
291
- }, options));
292
- }
293
- },
294
- UnhandledJobError: class UnhandledJobError extends GhostError {
295
- constructor(options) {
296
- super(merge({
297
- errorType: 'UnhandledJobError',
298
- message: 'Processed job threw an unhandled error',
299
- level: 'critical'
300
- }, options));
301
- }
302
- },
303
- NoContentError: class NoContentError extends GhostError {
304
- constructor(options) {
305
- super(merge({
306
- errorType: 'NoContentError',
307
- statusCode: 204,
308
- hideStack: true
309
- }, options));
310
- }
311
- },
312
- ConflictError: class ConflictError extends GhostError {
313
- constructor(options) {
314
- super(merge({
315
- errorType: 'ConflictError',
316
- statusCode: 409,
317
- message: 'The server has encountered an conflict.'
318
- }, options));
319
- }
320
- },
321
- MigrationError: class MigrationError extends GhostError {
322
- constructor(options) {
323
- super(merge({
324
- errorType: 'MigrationError',
325
- message: 'An error has occurred applying a database migration.',
326
- level: 'critical'
327
- }, options));
328
- }
329
- }
330
- };
331
-
332
- module.exports = ghostErrors;
333
-
334
- const ghostErrorsWithBase = Object.assign({}, ghostErrors, {GhostError});
335
- module.exports.utils = {
336
- serialize: utils.serialize.bind(ghostErrorsWithBase),
337
- deserialize: utils.deserialize.bind(ghostErrorsWithBase),
338
- isGhostError: utils.isGhostError.bind(ghostErrorsWithBase),
339
- prepareStackForUser: utils.prepareStackForUser
340
- };