1 | 'use strict';
|
2 |
|
3 | const _ = require('lodash');
|
4 |
|
5 | class MicrokitError extends Error {
|
6 | constructor(options) {
|
7 | super();
|
8 |
|
9 |
|
10 | Error.captureStackTrace(this, this.constructor);
|
11 |
|
12 |
|
13 | if (!_.has(options, 'name')) {
|
14 | throw new Error('error name must be defined');
|
15 | }
|
16 |
|
17 |
|
18 | if (!_.has(options, 'code')) {
|
19 | throw new Error('error code must be defined');
|
20 | }
|
21 |
|
22 | if (_.has(options, 'extra') && !_.isObject(options.extra)) {
|
23 | throw new Error('extra parameters must be object');
|
24 | }
|
25 |
|
26 | this._options = options;
|
27 | }
|
28 |
|
29 | |
30 |
|
31 |
|
32 | get id() {
|
33 | if (this._id) {
|
34 | return this._id;
|
35 | }
|
36 |
|
37 | const length = 48;
|
38 | this._id = Math.round(
|
39 | (Math.pow(36, length + 1) -
|
40 | Math.random() * Math.pow(36, length))
|
41 | ).toString(36).slice(1);
|
42 |
|
43 | return this._id;
|
44 | }
|
45 |
|
46 | |
47 |
|
48 |
|
49 | get name() {
|
50 | return this._options.name;
|
51 | }
|
52 |
|
53 | |
54 |
|
55 |
|
56 | get code() {
|
57 | return this._options.code;
|
58 | }
|
59 |
|
60 | |
61 |
|
62 |
|
63 | get error() {
|
64 | return this._options.error;
|
65 | }
|
66 |
|
67 | |
68 |
|
69 |
|
70 | get message() {
|
71 | const extra = _.extend(_.clone(this.extra), {code: this.code});
|
72 | return this._options.message ?
|
73 | `[${this._toKVString(extra)}] ${this._options.message}`:
|
74 | `[${this._toKVString(extra)}]`;
|
75 | }
|
76 |
|
77 | |
78 |
|
79 |
|
80 | get extra() {
|
81 | return this._options.extra || {};
|
82 | }
|
83 |
|
84 | |
85 |
|
86 |
|
87 | get meta() {
|
88 | return this._options.meta;
|
89 | }
|
90 |
|
91 | |
92 |
|
93 |
|
94 | toJSON() {
|
95 | return {
|
96 | id: this.id,
|
97 | code: this.code,
|
98 | message: this._options.message,
|
99 | extra: this.extra
|
100 | };
|
101 | }
|
102 |
|
103 | _toKVString(obj) {
|
104 | return _.join(_.map(obj, (val, key) => `${key}=${val}`), ' ');
|
105 | }
|
106 | }
|
107 |
|
108 | MicrokitError.extend = function(baseOptions) {
|
109 | return class childError extends this {
|
110 | constructor(options) {
|
111 | super(_.defaultsDeep(options, baseOptions));
|
112 | }
|
113 | };
|
114 | }
|
115 |
|
116 | module.exports = {
|
117 | Error: MicrokitError,
|
118 | NotImplementedError: MicrokitError.extend({
|
119 | name: 'NotImplementedError',
|
120 | meta: {httpCode: 501}
|
121 | }),
|
122 | InternalError: MicrokitError.extend({
|
123 | name: 'InternalError',
|
124 | meta: {httpCode: 500}
|
125 | }),
|
126 | NotFoundError: MicrokitError.extend({
|
127 | name: 'NotFoundError',
|
128 | meta: {httpCode: 404}
|
129 | }),
|
130 | UnautorizedError: MicrokitError.extend({
|
131 | name: 'UnautorizedError',
|
132 | meta: {httpCode: 401}
|
133 | }),
|
134 | ConnectionError: MicrokitError.extend({
|
135 | name: 'ConnectionError',
|
136 | meta: {httpCode: 504}
|
137 | }),
|
138 | ValidationError: MicrokitError.extend({
|
139 | name: 'ValidationError',
|
140 | meta: {httpCode: 400}
|
141 | }),
|
142 | ConflictError: MicrokitError.extend({
|
143 | name: 'ConflictError',
|
144 | meta: {httpCode: 409}
|
145 | }),
|
146 | LogicalError: MicrokitError.extend({
|
147 | name: 'LogicalError',
|
148 | meta: {httpCode: 422}
|
149 | })
|
150 | };
|