forked from kriasoft/graphql-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.js
48 lines (40 loc) · 1.16 KB
/
errors.js
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
/**
* Copyright © 2016-present Kriasoft.
*
* This source code is licensed under the MIT license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
/* @flow */
import type { ValidationErrorEntry } from './types';
// TODO: Log the error to Google Stackdriver, Rollbar etc.
function report(error: Error) {
// eslint-disable-next-line no-console
console.error(error);
}
export class ValidationError extends Error {
code = 400;
state: any;
constructor(errors: Array<ValidationErrorEntry>) {
super('The request is invalid.');
this.state = errors.reduce((result, error) => {
if (Object.prototype.hasOwnProperty.call(result, error.key)) {
result[error.key].push(error.message);
} else {
Object.defineProperty(result, error.key, {
value: [error.message],
enumerable: true,
});
}
return result;
}, {});
}
}
export class UnauthorizedError extends Error {
code = 401;
message = this.message || 'Anonymous access is denied.';
}
export class ForbiddenError extends Error {
code = 403;
message = this.message || 'Access is denied.';
}
export default { report };