@emoyly/problem 4.1.12 → 5.0.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/defaults/4xx.js +1 -1
- package/defaults/5xx.js +1 -1
- package/index.d.ts +1 -0
- package/index.js +6 -1
- package/middleware/axios.d.ts +2 -2
- package/middleware/base.d.ts +2 -2
- package/middleware/base.js +18 -11
- package/middleware/express.d.ts +2 -2
- package/package.json +20 -12
- package/parsers/axios.d.ts +1 -1
- package/parsers/axios.js +14 -14
- package/parsers/jsonwebtoken.d.ts +1 -1
- package/parsers/mikroorm.d.ts +1 -1
- package/parsers/tsoa.d.ts +1 -1
- package/problem.d.ts +4 -3
- package/problem.js +8 -5
- package/tsconfig.tsbuildinfo +1 -3797
- package/typings/codes.d.ts +5 -0
- package/typings/{misc.js → codes.js} +0 -0
- package/typings/events.d.ts +1 -1
- package/typings/index.d.ts +1 -1
- package/typings/index.js +6 -2
- package/typings/middleware.d.ts +1 -1
- package/typings/parser.d.ts +1 -8
- package/typings/problem.d.ts +4 -3
- package/util/defaults.d.ts +4 -0
- package/util/defaults.js +7 -0
- package/util/getProblems.d.ts +2 -2
- package/util/getProblems.js +7 -4
- package/util/isProblemArray.d.ts +2 -0
- package/util/isProblemArray.js +10 -0
- package/util/misc.d.ts +2 -0
- package/util/misc.js +43 -0
- package/util/version.d.ts +1 -1
- package/util/version.js +1 -1
- package/parsers/http.d.ts +0 -3
- package/parsers/http.js +0 -15
- package/typings/misc.d.ts +0 -3
package/defaults/4xx.js
CHANGED
package/defaults/5xx.js
CHANGED
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -32,3 +36,4 @@ exports.defaults = __importStar(require("./defaults"));
|
|
|
32
36
|
var events_1 = require("./util/events");
|
|
33
37
|
Object.defineProperty(exports, "events", { enumerable: true, get: function () { return __importDefault(events_1).default; } });
|
|
34
38
|
__exportStar(require("./typings"), exports);
|
|
39
|
+
__exportStar(require("./util/isProblemArray"), exports);
|
package/middleware/axios.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import type { PartialMiddlewareOptions } from '../typings/middleware';
|
|
2
2
|
import { MiddlewareBase } from './base';
|
|
3
|
-
|
|
3
|
+
type InterceptorArray = [undefined, (error: unknown) => Promise<never>];
|
|
4
4
|
export declare class AxiosMiddleware extends MiddlewareBase {
|
|
5
5
|
name: string;
|
|
6
6
|
constructor(options?: PartialMiddlewareOptions);
|
|
7
|
-
interceptor: (error:
|
|
7
|
+
interceptor: (error: unknown) => Promise<never>;
|
|
8
8
|
/**
|
|
9
9
|
* @example instance.interceptors.response.use(...middleware.use())
|
|
10
10
|
*/
|
package/middleware/base.d.ts
CHANGED
|
@@ -15,10 +15,10 @@ export declare abstract class MiddlewareBase {
|
|
|
15
15
|
/**
|
|
16
16
|
* Call this function when you want to use the middleware function
|
|
17
17
|
*/
|
|
18
|
-
abstract use():
|
|
18
|
+
abstract use(): unknown;
|
|
19
19
|
/**
|
|
20
20
|
* Parse input using parsers
|
|
21
21
|
*/
|
|
22
|
-
parse: (input:
|
|
22
|
+
parse: (input: unknown) => Promise<Problem[]>;
|
|
23
23
|
fallback: ProblemOpts;
|
|
24
24
|
}
|
package/middleware/base.js
CHANGED
|
@@ -27,18 +27,25 @@ class MiddlewareBase {
|
|
|
27
27
|
*/
|
|
28
28
|
this.parse = (input) => __awaiter(this, void 0, void 0, function* () {
|
|
29
29
|
const problems = [];
|
|
30
|
-
const prob = getProblems_1.getProblems(input);
|
|
31
|
-
if (prob)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
const prob = (0, getProblems_1.getProblems)(input);
|
|
31
|
+
if (!prob) {
|
|
32
|
+
for (const parse of this.options.parsers) {
|
|
33
|
+
const resp = parse(input);
|
|
34
|
+
if (!resp.length)
|
|
35
|
+
continue;
|
|
36
|
+
problems.push(...resp);
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
if (!problems.length && this.enableFallback) {
|
|
40
|
+
problems.push(new problem_1.Problem(Object.assign(Object.assign({}, this.fallback), { 'errorObject': input, 'stack': (typeof input === 'object' && input !== null && 'stack' in input && typeof input.stack === 'string') ? input.stack : undefined })));
|
|
41
|
+
}
|
|
39
42
|
}
|
|
40
|
-
|
|
41
|
-
problems.push(
|
|
43
|
+
else {
|
|
44
|
+
problems.push(...prob);
|
|
45
|
+
}
|
|
46
|
+
for (const p of problems) {
|
|
47
|
+
if (!p.middleware)
|
|
48
|
+
p.middleware = this.name;
|
|
42
49
|
}
|
|
43
50
|
return problems;
|
|
44
51
|
});
|
package/middleware/express.d.ts
CHANGED
|
@@ -3,13 +3,13 @@ import type { PartialMiddlewareOptions } from '../typings/middleware';
|
|
|
3
3
|
import { MiddlewareBase } from './base';
|
|
4
4
|
export declare class ExpressMiddleware extends MiddlewareBase {
|
|
5
5
|
name: string;
|
|
6
|
-
middleware: (error:
|
|
6
|
+
middleware: (error: unknown, req: Request, res: Response, next: NextFunction) => void;
|
|
7
7
|
/**
|
|
8
8
|
* This function applies the middleware to your Express application. Put this at the very end of your middleware/routes.
|
|
9
9
|
* If you want to also handle 404 errors, check out the notFound middleware function.
|
|
10
10
|
* @example app.use(middleware.use())
|
|
11
11
|
*/
|
|
12
|
-
use: () => (error:
|
|
12
|
+
use: () => (error: unknown, req: Request, res: Response, next: NextFunction) => void;
|
|
13
13
|
constructor(options?: PartialMiddlewareOptions);
|
|
14
14
|
/**
|
|
15
15
|
* Put this right before the middleware.use() to catch all 404 errors and respond with a Problem instead of the default Express response
|
package/package.json
CHANGED
|
@@ -1,27 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@emoyly/problem",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "A simple error library based around the RFC-7807 standard with optional support for Sentry.io and Express",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
7
|
"repository": "https://github.com/emoyly/problem",
|
|
8
8
|
"author": "Emil Petersen <emoyly@gmail.com>",
|
|
9
9
|
"license": "MIT",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"prepublish": "node ../scripts/ensureCorrectVersion.js"
|
|
12
|
+
},
|
|
10
13
|
"devDependencies": {
|
|
11
|
-
"@mikro-orm/core": "^
|
|
12
|
-
"@types/express": "^4.17.
|
|
13
|
-
"@types/jsonwebtoken": "^
|
|
14
|
-
"@types/node": "^
|
|
15
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
16
|
-
"@typescript-eslint/parser": "^
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
20
|
-
"
|
|
21
|
-
"
|
|
14
|
+
"@mikro-orm/core": "^5.6.11",
|
|
15
|
+
"@types/express": "^4.17.17",
|
|
16
|
+
"@types/jsonwebtoken": "^9.0.1",
|
|
17
|
+
"@types/node": "^18.14.0",
|
|
18
|
+
"@typescript-eslint/eslint-plugin": "^5.53.0",
|
|
19
|
+
"@typescript-eslint/parser": "^5.53.0",
|
|
20
|
+
"axios": "^1.3.3",
|
|
21
|
+
"eslint": "^8.34.0",
|
|
22
|
+
"fs-extra": "^11.1.0",
|
|
23
|
+
"jsonwebtoken": "^9.0.0",
|
|
24
|
+
"tsoa": "^5.1.1",
|
|
25
|
+
"typescript": "^4.9.5"
|
|
22
26
|
},
|
|
23
27
|
"peerDependencies": {
|
|
24
28
|
"@mikro-orm/core": "^4.5.9",
|
|
29
|
+
"axios": "*",
|
|
25
30
|
"jsonwebtoken": "^8.5.1",
|
|
26
31
|
"tsoa": "^3.7.0"
|
|
27
32
|
},
|
|
@@ -29,6 +34,9 @@
|
|
|
29
34
|
"@mikro-orm/core": {
|
|
30
35
|
"optional": true
|
|
31
36
|
},
|
|
37
|
+
"axios": {
|
|
38
|
+
"optional": true
|
|
39
|
+
},
|
|
32
40
|
"jsonwebtoken": {
|
|
33
41
|
"optional": true
|
|
34
42
|
},
|
package/parsers/axios.d.ts
CHANGED
package/parsers/axios.js
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const axios_1 = require("axios");
|
|
3
4
|
const others_1 = require("../defaults/others");
|
|
4
5
|
const problem_1 = require("../problem");
|
|
5
6
|
const getProblems_1 = require("../util/getProblems");
|
|
6
|
-
const
|
|
7
|
+
const misc_1 = require("../util/misc");
|
|
7
8
|
const parse = (input) => {
|
|
8
|
-
var _a, _b, _c, _d, _e
|
|
9
|
+
var _a, _b, _c, _d, _e;
|
|
10
|
+
if (typeof input !== 'object' || input == null)
|
|
11
|
+
return [];
|
|
12
|
+
if (!(0, axios_1.isAxiosError)(input))
|
|
13
|
+
return [];
|
|
9
14
|
const request = input.request;
|
|
10
15
|
if ((_a = input === null || input === void 0 ? void 0 : input.response) === null || _a === void 0 ? void 0 : _a.data) {
|
|
11
|
-
const problems = getProblems_1.getProblems((_b = input === null || input === void 0 ? void 0 : input.response) === null || _b === void 0 ? void 0 : _b.data);
|
|
16
|
+
const problems = (0, getProblems_1.getProblems)((_b = input === null || input === void 0 ? void 0 : input.response) === null || _b === void 0 ? void 0 : _b.data);
|
|
12
17
|
if (problems)
|
|
13
18
|
return problems;
|
|
14
19
|
}
|
|
15
20
|
if (input === null || input === void 0 ? void 0 : input.response) {
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
'
|
|
19
|
-
'headers': input.response.headers,
|
|
20
|
-
'statusCode': input.response.status,
|
|
21
|
-
url
|
|
22
|
-
});
|
|
23
|
-
if (problem)
|
|
24
|
-
return [problem];
|
|
21
|
+
const opts = (0, misc_1.getHttpError)(input.response.status);
|
|
22
|
+
if (opts)
|
|
23
|
+
return [new problem_1.Problem(Object.assign(Object.assign({}, opts), { 'instance': (_c = input === null || input === void 0 ? void 0 : input.config) === null || _c === void 0 ? void 0 : _c.url, 'stack': input === null || input === void 0 ? void 0 : input.stack, 'errorObject': input }))];
|
|
25
24
|
}
|
|
25
|
+
// TODO: Fix this, this seems bad
|
|
26
26
|
if (request) {
|
|
27
27
|
const isNode = typeof process !== 'undefined' && process.versions && process.versions.node;
|
|
28
28
|
const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
|
|
29
29
|
if (isBrowser && request instanceof XMLHttpRequest) {
|
|
30
|
-
return [new problem_1.Problem(Object.assign(Object.assign({}, others_1.otherErrors.networkError), { 'instance': (
|
|
30
|
+
return [new problem_1.Problem(Object.assign(Object.assign({}, others_1.otherErrors.networkError), { 'instance': (_d = input === null || input === void 0 ? void 0 : input.config) === null || _d === void 0 ? void 0 : _d.url }))];
|
|
31
31
|
}
|
|
32
32
|
// Probably a ClientRequest
|
|
33
33
|
if (isNode && 'pipe' in request && 'destroy' in request) {
|
|
34
|
-
return [new problem_1.Problem(Object.assign(Object.assign({}, others_1.otherErrors.networkError), { 'instance': (
|
|
34
|
+
return [new problem_1.Problem(Object.assign(Object.assign({}, others_1.otherErrors.networkError), { 'instance': (_e = input === null || input === void 0 ? void 0 : input.config) === null || _e === void 0 ? void 0 : _e.url }))];
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
return [];
|
package/parsers/mikroorm.d.ts
CHANGED
package/parsers/tsoa.d.ts
CHANGED
package/problem.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IProblem, ProblemOpts, JsonProblem } from './typings/problem';
|
|
1
|
+
import type { IProblem, ProblemOpts, JsonProblem } from './typings/problem';
|
|
2
2
|
export declare class Problem extends Error implements IProblem {
|
|
3
3
|
type: string;
|
|
4
4
|
title: string;
|
|
@@ -7,7 +7,8 @@ export declare class Problem extends Error implements IProblem {
|
|
|
7
7
|
instance: string;
|
|
8
8
|
errorObject?: unknown;
|
|
9
9
|
__problemVersion: string;
|
|
10
|
-
data:
|
|
11
|
-
|
|
10
|
+
data: unknown;
|
|
11
|
+
middleware?: string;
|
|
12
|
+
constructor({ type, title, status, detail, instance, stack, errorObject, data, middleware }: ProblemOpts);
|
|
12
13
|
toObject: () => JsonProblem;
|
|
13
14
|
}
|
package/problem.js
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Problem = void 0;
|
|
4
|
+
const defaults_1 = require("./util/defaults");
|
|
4
5
|
const version_1 = require("./util/version");
|
|
5
6
|
class Problem extends Error {
|
|
6
|
-
constructor({ type, title, status, detail, instance, stack, errorObject, data }) {
|
|
7
|
+
constructor({ type, title, status, detail, instance, stack, errorObject, data, middleware }) {
|
|
7
8
|
var _a;
|
|
8
9
|
super((_a = detail !== null && detail !== void 0 ? detail : title) !== null && _a !== void 0 ? _a : 'HTTP Problem Details');
|
|
9
|
-
this.type =
|
|
10
|
-
this.title =
|
|
10
|
+
this.type = defaults_1.defaultType;
|
|
11
|
+
this.title = defaults_1.defaultTitle;
|
|
11
12
|
this.status = 500;
|
|
12
|
-
this.detail =
|
|
13
|
-
this.instance =
|
|
13
|
+
this.detail = defaults_1.defaultDetail;
|
|
14
|
+
this.instance = defaults_1.defaultInstance;
|
|
14
15
|
this.__problemVersion = version_1.version;
|
|
15
16
|
this.data = undefined;
|
|
16
17
|
this.toObject = () => {
|
|
@@ -35,6 +36,8 @@ class Problem extends Error {
|
|
|
35
36
|
this.instance = instance;
|
|
36
37
|
if (errorObject)
|
|
37
38
|
this.errorObject = errorObject;
|
|
39
|
+
if (middleware)
|
|
40
|
+
this.middleware = middleware;
|
|
38
41
|
if (stack) {
|
|
39
42
|
this.stack = stack;
|
|
40
43
|
}
|