@tryghost/mw-error-handler 0.1.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.
- package/LICENSE +21 -0
- package/README.md +39 -0
- package/index.js +1 -0
- package/lib/mw-error-handler.js +198 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013-2021 Ghost Foundation
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Mw Error Handler
|
|
2
|
+
|
|
3
|
+
## Install
|
|
4
|
+
|
|
5
|
+
`npm install @tryghost/mw-error-handler --save`
|
|
6
|
+
|
|
7
|
+
or
|
|
8
|
+
|
|
9
|
+
`yarn add @tryghost/mw-error-handler`
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## Develop
|
|
16
|
+
|
|
17
|
+
This is a mono repository, managed with [lerna](https://lernajs.io/).
|
|
18
|
+
|
|
19
|
+
Follow the instructions for the top-level repo.
|
|
20
|
+
1. `git clone` this repo & `cd` into it as usual
|
|
21
|
+
2. Run `yarn` to install top-level dependencies.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
## Run
|
|
25
|
+
|
|
26
|
+
- `yarn dev`
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
## Test
|
|
30
|
+
|
|
31
|
+
- `yarn lint` run just eslint
|
|
32
|
+
- `yarn test` run lint and tests
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# Copyright & License
|
|
38
|
+
|
|
39
|
+
Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE).
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./lib/mw-error-handler');
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
const _ = require('lodash');
|
|
2
|
+
const debug = require('@tryghost/debug')('error-handler');
|
|
3
|
+
const errors = require('@tryghost/errors');
|
|
4
|
+
const tpl = require('@tryghost/tpl');
|
|
5
|
+
|
|
6
|
+
const messages = {
|
|
7
|
+
pageNotFound: 'Page not found',
|
|
8
|
+
resourceNotFound: 'Resource not found',
|
|
9
|
+
actions: {
|
|
10
|
+
images: {
|
|
11
|
+
upload: 'upload image'
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
userMessages: {
|
|
15
|
+
BookshelfRelationsError: 'Database error, cannot {action}.',
|
|
16
|
+
InternalServerError: 'Internal server error, cannot {action}.',
|
|
17
|
+
IncorrectUsageError: 'Incorrect usage error, cannot {action}.',
|
|
18
|
+
NotFoundError: 'Resource not found error, cannot {action}.',
|
|
19
|
+
BadRequestError: 'Request not understood error, cannot {action}.',
|
|
20
|
+
UnauthorizedError: 'Authorisation error, cannot {action}.',
|
|
21
|
+
NoPermissionError: 'Permission error, cannot {action}.',
|
|
22
|
+
ValidationError: 'Validation error, cannot {action}.',
|
|
23
|
+
UnsupportedMediaTypeError: 'Unsupported media error, cannot {action}.',
|
|
24
|
+
TooManyRequestsError: 'Too many requests error, cannot {action}.',
|
|
25
|
+
MaintenanceError: 'Server down for maintenance, cannot {action}.',
|
|
26
|
+
MethodNotAllowedError: 'Method not allowed, cannot {action}.',
|
|
27
|
+
RequestEntityTooLargeError: 'Request too large, cannot {action}.',
|
|
28
|
+
TokenRevocationError: 'Token is not available, cannot {action}.',
|
|
29
|
+
VersionMismatchError: 'Version mismatch error, cannot {action}.',
|
|
30
|
+
DataExportError: 'Error exporting content.',
|
|
31
|
+
DataImportError: 'Duplicated entry, cannot save {action}.',
|
|
32
|
+
DatabaseVersionError: 'Database version compatibility error, cannot {action}.',
|
|
33
|
+
EmailError: 'Error sending email!',
|
|
34
|
+
ThemeValidationError: 'Theme validation error, cannot {action}.',
|
|
35
|
+
HostLimitError: 'Host Limit error, cannot {action}.',
|
|
36
|
+
DisabledFeatureError: 'Theme validation error, the {{{helperName}}} helper is not available. Cannot {action}.',
|
|
37
|
+
UpdateCollisionError: 'Saving failed! Someone else is editing this post.'
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Get an error ready to be shown the the user
|
|
43
|
+
*/
|
|
44
|
+
module.exports.prepareError = (err, req, res, next) => {
|
|
45
|
+
debug(err);
|
|
46
|
+
|
|
47
|
+
if (Array.isArray(err)) {
|
|
48
|
+
err = err[0];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!errors.utils.isGhostError(err)) {
|
|
52
|
+
// We need a special case for 404 errors
|
|
53
|
+
if (err.statusCode && err.statusCode === 404) {
|
|
54
|
+
err = new errors.NotFoundError({
|
|
55
|
+
err: err
|
|
56
|
+
});
|
|
57
|
+
} else if (err.stack.match(/node_modules\/handlebars\//)) {
|
|
58
|
+
// Temporary handling of theme errors from handlebars
|
|
59
|
+
// @TODO remove this when #10496 is solved properly
|
|
60
|
+
err = new errors.IncorrectUsageError({
|
|
61
|
+
err: err,
|
|
62
|
+
message: err.message,
|
|
63
|
+
statusCode: err.statusCode
|
|
64
|
+
});
|
|
65
|
+
} else {
|
|
66
|
+
err = new errors.InternalServerError({
|
|
67
|
+
err: err,
|
|
68
|
+
message: err.message,
|
|
69
|
+
statusCode: err.statusCode
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// used for express logging middleware see core/server/app.js
|
|
75
|
+
req.err = err;
|
|
76
|
+
|
|
77
|
+
// alternative for res.status();
|
|
78
|
+
res.statusCode = err.statusCode;
|
|
79
|
+
|
|
80
|
+
err = err.prepareErrorForUser();
|
|
81
|
+
|
|
82
|
+
// never cache errors
|
|
83
|
+
res.set({
|
|
84
|
+
'Cache-Control': 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0'
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
next(err);
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const jsonErrorRenderer = (err, req, res, next) => { // eslint-disable-line no-unused-vars
|
|
91
|
+
res.json({
|
|
92
|
+
errors: [{
|
|
93
|
+
message: err.message,
|
|
94
|
+
context: err.context,
|
|
95
|
+
help: err.help,
|
|
96
|
+
errorType: err.errorType,
|
|
97
|
+
errorDetails: err.errorDetails,
|
|
98
|
+
ghostErrorCode: err.ghostErrorCode
|
|
99
|
+
}]
|
|
100
|
+
});
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const jsonErrorRendererV2 = (err, req, res, next) => { // eslint-disable-line no-unused-vars
|
|
104
|
+
const userError = prepareUserMessage(err, req);
|
|
105
|
+
|
|
106
|
+
res.json({
|
|
107
|
+
errors: [{
|
|
108
|
+
message: userError.message || null,
|
|
109
|
+
context: userError.context || null,
|
|
110
|
+
type: err.errorType || null,
|
|
111
|
+
details: err.errorDetails || null,
|
|
112
|
+
property: err.property || null,
|
|
113
|
+
help: err.help || null,
|
|
114
|
+
code: err.code || null,
|
|
115
|
+
id: err.id || null
|
|
116
|
+
}]
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const prepareUserMessage = (err, res) => {
|
|
121
|
+
const userError = {
|
|
122
|
+
message: err.message,
|
|
123
|
+
context: err.context
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const docName = _.get(res, 'frameOptions.docName');
|
|
127
|
+
const method = _.get(res, 'frameOptions.method');
|
|
128
|
+
|
|
129
|
+
if (docName && method) {
|
|
130
|
+
let action;
|
|
131
|
+
|
|
132
|
+
const actionMap = {
|
|
133
|
+
browse: 'list',
|
|
134
|
+
read: 'read',
|
|
135
|
+
add: 'save',
|
|
136
|
+
edit: 'edit',
|
|
137
|
+
destroy: 'delete'
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
if (_.get(messages.actions, [docName, method])) {
|
|
141
|
+
action = tpl(messages.actions[docName][method]);
|
|
142
|
+
} else if (Object.keys(actionMap).includes(method)) {
|
|
143
|
+
let resource = docName;
|
|
144
|
+
|
|
145
|
+
if (method !== 'browse') {
|
|
146
|
+
resource = resource.replace(/s$/, '');
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
action = `${actionMap[method]} ${resource}`;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (action) {
|
|
153
|
+
if (err.context) {
|
|
154
|
+
userError.context = `${err.message} ${err.context}`;
|
|
155
|
+
} else {
|
|
156
|
+
userError.context = err.message;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
userError.message = tpl(messages.userMessages[err.name], {action: action});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return userError;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
module.exports.resourceNotFound = (req, res, next) => {
|
|
167
|
+
next(new errors.NotFoundError({message: tpl(messages.resourceNotFound)}));
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
module.exports.pageNotFound = (req, res, next) => {
|
|
171
|
+
next(new errors.NotFoundError({message: tpl(messages.pageNotFound)}));
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
module.exports.handleJSONResponse = sentry => [
|
|
175
|
+
// Make sure the error can be served
|
|
176
|
+
module.exports.prepareError,
|
|
177
|
+
// Handle the error in Sentry
|
|
178
|
+
sentry.errorHandler,
|
|
179
|
+
// Render the error using JSON format
|
|
180
|
+
jsonErrorRenderer
|
|
181
|
+
];
|
|
182
|
+
|
|
183
|
+
module.exports.handleJSONResponseV2 = sentry => [
|
|
184
|
+
// Make sure the error can be served
|
|
185
|
+
module.exports.prepareError,
|
|
186
|
+
// Handle the error in Sentry
|
|
187
|
+
sentry.errorHandler,
|
|
188
|
+
// Render the error using JSON format
|
|
189
|
+
jsonErrorRendererV2
|
|
190
|
+
];
|
|
191
|
+
|
|
192
|
+
module.exports.handleHTMLResponse = sentry => [
|
|
193
|
+
// Make sure the error can be served
|
|
194
|
+
module.exports.prepareError,
|
|
195
|
+
// Handle the error in Sentry
|
|
196
|
+
sentry.errorHandler
|
|
197
|
+
];
|
|
198
|
+
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tryghost/mw-error-handler",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"repository": "https://github.com/TryGhost/Utils/tree/main/packages/mw-error-handler",
|
|
5
|
+
"author": "Ghost Foundation",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"dev": "echo \"Implement me!\"",
|
|
10
|
+
"test": "NODE_ENV=testing c8 --check-coverage mocha './test/**/*.test.js'",
|
|
11
|
+
"lint": "eslint . --ext .js --cache",
|
|
12
|
+
"posttest": "yarn lint"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"index.js",
|
|
16
|
+
"lib"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"c8": "7.10.0",
|
|
23
|
+
"mocha": "9.1.3",
|
|
24
|
+
"should": "13.2.3",
|
|
25
|
+
"sinon": "12.0.1"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@tryghost/debug": "^0.1.9",
|
|
29
|
+
"@tryghost/tpl": "^0.1.8"
|
|
30
|
+
},
|
|
31
|
+
"gitHead": "b1b11d0f5f788981c846984346c420aa50d55899"
|
|
32
|
+
}
|