@keq-request/exception 5.0.0-alpha.30 → 5.0.0-alpha.32
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/CHANGELOG.md +18 -0
- package/dist/create-exception-by-status-code.d.ts +2 -0
- package/dist/create-exception-by-status-code.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +62 -54
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +62 -54
- package/dist/index.mjs.map +1 -1
- package/dist/validate-status-code.d.ts.map +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 5.0.0-alpha.32
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 74803c8: export `createExceptionByStatusCode` to create HTTP exceptions from a `Response` object
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [f194c41]
|
|
12
|
+
- Updated dependencies [b8d02ca]
|
|
13
|
+
- keq@5.0.0-alpha.32
|
|
14
|
+
|
|
15
|
+
## 5.0.0-alpha.31
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- keq@5.0.0-alpha.31
|
|
20
|
+
|
|
3
21
|
## 5.0.0-alpha.30
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-exception-by-status-code.d.ts","sourceRoot":"","sources":["../src/create-exception-by-status-code.ts"],"names":[],"mappings":"AAwBA,wBAAgB,2BAA2B,CAAC,QAAQ,EAAE,QAAQ,GAAG,KAAK,CA8DrE"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA;AACpC,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAA;AACpC,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,sCAAsC,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
catchException: () => catchException,
|
|
24
|
+
createExceptionByStatusCode: () => createExceptionByStatusCode,
|
|
24
25
|
throwException: () => throwException,
|
|
25
26
|
validateStatusCode: () => validateStatusCode
|
|
26
27
|
});
|
|
@@ -45,68 +46,75 @@ function catchException(handler) {
|
|
|
45
46
|
};
|
|
46
47
|
}
|
|
47
48
|
|
|
48
|
-
// src/
|
|
49
|
+
// src/create-exception-by-status-code.ts
|
|
49
50
|
var import_keq = require("keq");
|
|
51
|
+
function createExceptionByStatusCode(response) {
|
|
52
|
+
const { status, statusText } = response;
|
|
53
|
+
if (status >= 400 && status < 500) {
|
|
54
|
+
switch (status) {
|
|
55
|
+
case 400:
|
|
56
|
+
return new import_keq.BadRequestException(statusText, { response });
|
|
57
|
+
case 401:
|
|
58
|
+
return new import_keq.UnauthorizedException(statusText, { response });
|
|
59
|
+
case 403:
|
|
60
|
+
return new import_keq.ForbiddenException(statusText, { response });
|
|
61
|
+
case 404:
|
|
62
|
+
return new import_keq.NotFoundedException(statusText, { response });
|
|
63
|
+
case 405:
|
|
64
|
+
return new import_keq.MethodNotAllowedException(statusText, { response });
|
|
65
|
+
case 406:
|
|
66
|
+
return new import_keq.NotAcceptableException(statusText, { response });
|
|
67
|
+
case 407:
|
|
68
|
+
return new import_keq.ProxyAuthenticationRequiredException(statusText, { response });
|
|
69
|
+
case 408:
|
|
70
|
+
return new import_keq.RequestTimeoutException(statusText, { response });
|
|
71
|
+
case 409:
|
|
72
|
+
return new import_keq.ConflictException(statusText, { response });
|
|
73
|
+
case 412:
|
|
74
|
+
return new import_keq.PreconditionFailedException(statusText, { response });
|
|
75
|
+
case 413:
|
|
76
|
+
return new import_keq.ContentTooLargeException(statusText, { response });
|
|
77
|
+
case 414:
|
|
78
|
+
return new import_keq.UriTooLongException(statusText, { response });
|
|
79
|
+
case 415:
|
|
80
|
+
return new import_keq.UnsupportedMediaTypeException(statusText, { response });
|
|
81
|
+
case 418:
|
|
82
|
+
return new import_keq.ImATeapotException(statusText, { response });
|
|
83
|
+
case 429:
|
|
84
|
+
return new import_keq.TooManyRequestsException(statusText, { response });
|
|
85
|
+
default:
|
|
86
|
+
return new import_keq.RequestException(status, statusText, { fatal: true, response });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (status >= 500) {
|
|
90
|
+
switch (status) {
|
|
91
|
+
case 500:
|
|
92
|
+
return new import_keq.InternalServerErrorException(statusText, { response });
|
|
93
|
+
case 501:
|
|
94
|
+
return new import_keq.NotImplementedException(statusText, { response });
|
|
95
|
+
case 502:
|
|
96
|
+
return new import_keq.BadGatewayException(statusText, { response });
|
|
97
|
+
case 503:
|
|
98
|
+
return new import_keq.ServiceUnavailableException(statusText, { response });
|
|
99
|
+
case 504:
|
|
100
|
+
return new import_keq.GatewayTimeoutException(statusText, { response });
|
|
101
|
+
default:
|
|
102
|
+
return new import_keq.RequestException(status, statusText, { fatal: false, response });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return new import_keq.RequestException(status, statusText, { fatal: false, response });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// src/validate-status-code.ts
|
|
50
109
|
function validateStatusCode() {
|
|
51
110
|
return async function validateStatusCode2(context, next) {
|
|
52
111
|
await next();
|
|
53
112
|
const response = context.response;
|
|
54
113
|
if (!response) return;
|
|
55
|
-
const { status
|
|
114
|
+
const { status } = response;
|
|
56
115
|
if (status >= 200 && status < 300) return;
|
|
57
116
|
if (status >= 300 && status < 400) return;
|
|
58
|
-
|
|
59
|
-
switch (status) {
|
|
60
|
-
case 400:
|
|
61
|
-
throw new import_keq.BadRequestException(statusText, { response });
|
|
62
|
-
case 401:
|
|
63
|
-
throw new import_keq.UnauthorizedException(statusText, { response });
|
|
64
|
-
case 403:
|
|
65
|
-
throw new import_keq.ForbiddenException(statusText, { response });
|
|
66
|
-
case 404:
|
|
67
|
-
throw new import_keq.NotFoundedException(statusText, { response });
|
|
68
|
-
case 405:
|
|
69
|
-
throw new import_keq.MethodNotAllowedException(statusText, { response });
|
|
70
|
-
case 406:
|
|
71
|
-
throw new import_keq.NotAcceptableException(statusText, { response });
|
|
72
|
-
case 407:
|
|
73
|
-
throw new import_keq.ProxyAuthenticationRequiredException(statusText, { response });
|
|
74
|
-
case 408:
|
|
75
|
-
throw new import_keq.RequestTimeoutException(statusText, { response });
|
|
76
|
-
case 409:
|
|
77
|
-
throw new import_keq.ConflictException(statusText, { response });
|
|
78
|
-
case 412:
|
|
79
|
-
throw new import_keq.PreconditionFailedException(statusText, { response });
|
|
80
|
-
case 413:
|
|
81
|
-
throw new import_keq.ContentTooLargeException(statusText, { response });
|
|
82
|
-
case 414:
|
|
83
|
-
throw new import_keq.UriTooLongException(statusText, { response });
|
|
84
|
-
case 415:
|
|
85
|
-
throw new import_keq.UnsupportedMediaTypeException(statusText, { response });
|
|
86
|
-
case 418:
|
|
87
|
-
throw new import_keq.ImATeapotException(statusText, { response });
|
|
88
|
-
case 429:
|
|
89
|
-
throw new import_keq.TooManyRequestsException(statusText, { response });
|
|
90
|
-
default:
|
|
91
|
-
throw new import_keq.RequestException(status, statusText, { fatal: true, response });
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
if (status >= 500) {
|
|
95
|
-
switch (status) {
|
|
96
|
-
case 500:
|
|
97
|
-
throw new import_keq.InternalServerErrorException(statusText, { response });
|
|
98
|
-
case 501:
|
|
99
|
-
throw new import_keq.NotImplementedException(statusText, { response });
|
|
100
|
-
case 502:
|
|
101
|
-
throw new import_keq.BadGatewayException(statusText, { response });
|
|
102
|
-
case 503:
|
|
103
|
-
throw new import_keq.ServiceUnavailableException(statusText, { response });
|
|
104
|
-
case 504:
|
|
105
|
-
throw new import_keq.GatewayTimeoutException(statusText, { response });
|
|
106
|
-
default:
|
|
107
|
-
throw new import_keq.RequestException(status, statusText, { fatal: false, response });
|
|
108
|
-
}
|
|
109
|
-
}
|
|
117
|
+
throw createExceptionByStatusCode(response);
|
|
110
118
|
};
|
|
111
119
|
}
|
|
112
120
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/throw-exception.ts","../src/catch-exception.ts","../src/validate-status-code.ts"],"sourcesContent":["export * from './throw-exception.js'\nexport * from './catch-exception.js'\nexport * from './validate-status-code.js'\n","import { KeqContext, KeqMiddleware } from 'keq'\nimport { Promisable } from 'type-fest'\n\n\nexport type Check = (ctx: KeqContext) => Promisable<void>\n\nexport function throwException(check: Check): KeqMiddleware {\n return async function throwException(ctx, next) {\n await next()\n\n await check(ctx)\n }\n}\n","import { KeqMiddleware } from 'keq'\nimport { Promisable } from 'type-fest'\n\n\nexport function catchException(handler: (e: unknown) => Promisable<void>): KeqMiddleware {\n return async function catchException(ctx, next) {\n try {\n await next()\n } catch (err) {\n await handler(err)\n }\n }\n}\n","import {\n BadGatewayException,\n BadRequestException,\n ConflictException,\n ForbiddenException,\n GatewayTimeoutException,\n InternalServerErrorException,\n
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/throw-exception.ts","../src/catch-exception.ts","../src/create-exception-by-status-code.ts","../src/validate-status-code.ts"],"sourcesContent":["export * from './throw-exception.js'\nexport * from './catch-exception.js'\nexport * from './validate-status-code.js'\nexport * from './create-exception-by-status-code.js'\n","import { KeqContext, KeqMiddleware } from 'keq'\nimport { Promisable } from 'type-fest'\n\n\nexport type Check = (ctx: KeqContext) => Promisable<void>\n\nexport function throwException(check: Check): KeqMiddleware {\n return async function throwException(ctx, next) {\n await next()\n\n await check(ctx)\n }\n}\n","import { KeqMiddleware } from 'keq'\nimport { Promisable } from 'type-fest'\n\n\nexport function catchException(handler: (e: unknown) => Promisable<void>): KeqMiddleware {\n return async function catchException(ctx, next) {\n try {\n await next()\n } catch (err) {\n await handler(err)\n }\n }\n}\n","import {\n BadGatewayException,\n BadRequestException,\n ConflictException,\n ForbiddenException,\n GatewayTimeoutException,\n InternalServerErrorException,\n NotAcceptableException,\n NotFoundedException,\n PreconditionFailedException,\n RequestException,\n ServiceUnavailableException,\n UnauthorizedException,\n ImATeapotException,\n MethodNotAllowedException,\n UriTooLongException,\n ContentTooLargeException,\n ProxyAuthenticationRequiredException,\n RequestTimeoutException,\n TooManyRequestsException,\n NotImplementedException,\n UnsupportedMediaTypeException,\n} from 'keq'\n\nexport function createExceptionByStatusCode(response: Response): Error {\n const { status, statusText } = response\n\n // 4xx client errors\n if (status >= 400 && status < 500) {\n switch (status) {\n case 400:\n return new BadRequestException(statusText, { response })\n case 401:\n return new UnauthorizedException(statusText, { response })\n case 403:\n return new ForbiddenException(statusText, { response })\n case 404:\n return new NotFoundedException(statusText, { response })\n case 405:\n return new MethodNotAllowedException(statusText, { response })\n case 406:\n return new NotAcceptableException(statusText, { response })\n case 407:\n return new ProxyAuthenticationRequiredException(statusText, { response })\n case 408:\n return new RequestTimeoutException(statusText, { response })\n case 409:\n return new ConflictException(statusText, { response })\n case 412:\n return new PreconditionFailedException(statusText, { response })\n case 413:\n return new ContentTooLargeException(statusText, { response })\n case 414:\n return new UriTooLongException(statusText, { response })\n case 415:\n return new UnsupportedMediaTypeException(statusText, { response })\n case 418:\n return new ImATeapotException(statusText, { response })\n case 429:\n return new TooManyRequestsException(statusText, { response })\n default:\n // Other 4xx errors, don't retry by default\n return new RequestException(status, statusText, { fatal: true, response })\n }\n }\n\n // 5xx server errors\n if (status >= 500) {\n switch (status) {\n case 500:\n return new InternalServerErrorException(statusText, { response })\n case 501:\n return new NotImplementedException(statusText, { response })\n case 502:\n return new BadGatewayException(statusText, { response })\n case 503:\n return new ServiceUnavailableException(statusText, { response })\n case 504:\n return new GatewayTimeoutException(statusText, { response })\n default:\n // Other 5xx errors, retry by default\n return new RequestException(status, statusText, { fatal: false, response })\n }\n }\n\n return new RequestException(status, statusText, { fatal: false, response })\n}\n","import { KeqMiddleware } from 'keq'\nimport { createExceptionByStatusCode } from './create-exception-by-status-code.js'\n\nexport function validateStatusCode(): KeqMiddleware {\n return async function validateStatusCode(context, next) {\n await next()\n\n const response = context.response\n if (!response) return\n\n const { status } = response\n\n // 2xx success status codes - no error\n if (status >= 200 && status < 300) return\n\n // 3xx redirection status codes - no error (handled by fetch)\n if (status >= 300 && status < 400) return\n\n throw createExceptionByStatusCode(response)\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMO,SAAS,eAAe,OAA6B;AAC1D,SAAO,eAAeA,gBAAe,KAAK,MAAM;AAC9C,UAAM,KAAK;AAEX,UAAM,MAAM,GAAG;AAAA,EACjB;AACF;;;ACRO,SAAS,eAAe,SAA0D;AACvF,SAAO,eAAeC,gBAAe,KAAK,MAAM;AAC9C,QAAI;AACF,YAAM,KAAK;AAAA,IACb,SAAS,KAAK;AACZ,YAAM,QAAQ,GAAG;AAAA,IACnB;AAAA,EACF;AACF;;;ACZA,iBAsBO;AAEA,SAAS,4BAA4B,UAA2B;AACrE,QAAM,EAAE,QAAQ,WAAW,IAAI;AAG/B,MAAI,UAAU,OAAO,SAAS,KAAK;AACjC,YAAQ,QAAQ;AAAA,MACd,KAAK;AACH,eAAO,IAAI,+BAAoB,YAAY,EAAE,SAAS,CAAC;AAAA,MACzD,KAAK;AACH,eAAO,IAAI,iCAAsB,Y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oB,YAAY,EAAE,SAAS,CAAC;AAAA,MACzD,KAAK;AACH,eAAO,IAAI,uCAA4B,YAAY,EAAE,SAAS,CAAC;AAAA,MACjE,KAAK;AACH,eAAO,IAAI,mCAAwB,YAAY,EAAE,SAAS,CAAC;AAAA,MAC7D;AAEE,eAAO,IAAI,4BAAiB,QAAQ,YAAY,EAAE,OAAO,OAAO,SAAS,CAAC;AAAA,IAC9E;AAAA,EACF;AAEA,SAAO,IAAI,4BAAiB,QAAQ,YAAY,EAAE,OAAO,OAAO,SAAS,CAAC;AAC5E;;;ACnFO,SAAS,qBAAoC;AAClD,SAAO,eAAeC,oBAAmB,SAAS,MAAM;AACtD,UAAM,KAAK;AAEX,UAAM,WAAW,QAAQ;AACzB,QAAI,CAAC,SAAU;AAEf,UAAM,EAAE,OAAO,IAAI;AAGnB,QAAI,UAAU,OAAO,SAAS,IAAK;AAGnC,QAAI,UAAU,OAAO,SAAS,IAAK;AAEnC,UAAM,4BAA4B,QAAQ;AAAA,EAC5C;AACF;","names":["throwException","catchException","validateStatusCode"]}
|
package/dist/index.mjs
CHANGED
|
@@ -17,7 +17,7 @@ function catchException(handler) {
|
|
|
17
17
|
};
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
// src/
|
|
20
|
+
// src/create-exception-by-status-code.ts
|
|
21
21
|
import {
|
|
22
22
|
BadGatewayException,
|
|
23
23
|
BadRequestException,
|
|
@@ -41,70 +41,78 @@ import {
|
|
|
41
41
|
NotImplementedException,
|
|
42
42
|
UnsupportedMediaTypeException
|
|
43
43
|
} from "keq";
|
|
44
|
+
function createExceptionByStatusCode(response) {
|
|
45
|
+
const { status, statusText } = response;
|
|
46
|
+
if (status >= 400 && status < 500) {
|
|
47
|
+
switch (status) {
|
|
48
|
+
case 400:
|
|
49
|
+
return new BadRequestException(statusText, { response });
|
|
50
|
+
case 401:
|
|
51
|
+
return new UnauthorizedException(statusText, { response });
|
|
52
|
+
case 403:
|
|
53
|
+
return new ForbiddenException(statusText, { response });
|
|
54
|
+
case 404:
|
|
55
|
+
return new NotFoundedException(statusText, { response });
|
|
56
|
+
case 405:
|
|
57
|
+
return new MethodNotAllowedException(statusText, { response });
|
|
58
|
+
case 406:
|
|
59
|
+
return new NotAcceptableException(statusText, { response });
|
|
60
|
+
case 407:
|
|
61
|
+
return new ProxyAuthenticationRequiredException(statusText, { response });
|
|
62
|
+
case 408:
|
|
63
|
+
return new RequestTimeoutException(statusText, { response });
|
|
64
|
+
case 409:
|
|
65
|
+
return new ConflictException(statusText, { response });
|
|
66
|
+
case 412:
|
|
67
|
+
return new PreconditionFailedException(statusText, { response });
|
|
68
|
+
case 413:
|
|
69
|
+
return new ContentTooLargeException(statusText, { response });
|
|
70
|
+
case 414:
|
|
71
|
+
return new UriTooLongException(statusText, { response });
|
|
72
|
+
case 415:
|
|
73
|
+
return new UnsupportedMediaTypeException(statusText, { response });
|
|
74
|
+
case 418:
|
|
75
|
+
return new ImATeapotException(statusText, { response });
|
|
76
|
+
case 429:
|
|
77
|
+
return new TooManyRequestsException(statusText, { response });
|
|
78
|
+
default:
|
|
79
|
+
return new RequestException(status, statusText, { fatal: true, response });
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (status >= 500) {
|
|
83
|
+
switch (status) {
|
|
84
|
+
case 500:
|
|
85
|
+
return new InternalServerErrorException(statusText, { response });
|
|
86
|
+
case 501:
|
|
87
|
+
return new NotImplementedException(statusText, { response });
|
|
88
|
+
case 502:
|
|
89
|
+
return new BadGatewayException(statusText, { response });
|
|
90
|
+
case 503:
|
|
91
|
+
return new ServiceUnavailableException(statusText, { response });
|
|
92
|
+
case 504:
|
|
93
|
+
return new GatewayTimeoutException(statusText, { response });
|
|
94
|
+
default:
|
|
95
|
+
return new RequestException(status, statusText, { fatal: false, response });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return new RequestException(status, statusText, { fatal: false, response });
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/validate-status-code.ts
|
|
44
102
|
function validateStatusCode() {
|
|
45
103
|
return async function validateStatusCode2(context, next) {
|
|
46
104
|
await next();
|
|
47
105
|
const response = context.response;
|
|
48
106
|
if (!response) return;
|
|
49
|
-
const { status
|
|
107
|
+
const { status } = response;
|
|
50
108
|
if (status >= 200 && status < 300) return;
|
|
51
109
|
if (status >= 300 && status < 400) return;
|
|
52
|
-
|
|
53
|
-
switch (status) {
|
|
54
|
-
case 400:
|
|
55
|
-
throw new BadRequestException(statusText, { response });
|
|
56
|
-
case 401:
|
|
57
|
-
throw new UnauthorizedException(statusText, { response });
|
|
58
|
-
case 403:
|
|
59
|
-
throw new ForbiddenException(statusText, { response });
|
|
60
|
-
case 404:
|
|
61
|
-
throw new NotFoundedException(statusText, { response });
|
|
62
|
-
case 405:
|
|
63
|
-
throw new MethodNotAllowedException(statusText, { response });
|
|
64
|
-
case 406:
|
|
65
|
-
throw new NotAcceptableException(statusText, { response });
|
|
66
|
-
case 407:
|
|
67
|
-
throw new ProxyAuthenticationRequiredException(statusText, { response });
|
|
68
|
-
case 408:
|
|
69
|
-
throw new RequestTimeoutException(statusText, { response });
|
|
70
|
-
case 409:
|
|
71
|
-
throw new ConflictException(statusText, { response });
|
|
72
|
-
case 412:
|
|
73
|
-
throw new PreconditionFailedException(statusText, { response });
|
|
74
|
-
case 413:
|
|
75
|
-
throw new ContentTooLargeException(statusText, { response });
|
|
76
|
-
case 414:
|
|
77
|
-
throw new UriTooLongException(statusText, { response });
|
|
78
|
-
case 415:
|
|
79
|
-
throw new UnsupportedMediaTypeException(statusText, { response });
|
|
80
|
-
case 418:
|
|
81
|
-
throw new ImATeapotException(statusText, { response });
|
|
82
|
-
case 429:
|
|
83
|
-
throw new TooManyRequestsException(statusText, { response });
|
|
84
|
-
default:
|
|
85
|
-
throw new RequestException(status, statusText, { fatal: true, response });
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
if (status >= 500) {
|
|
89
|
-
switch (status) {
|
|
90
|
-
case 500:
|
|
91
|
-
throw new InternalServerErrorException(statusText, { response });
|
|
92
|
-
case 501:
|
|
93
|
-
throw new NotImplementedException(statusText, { response });
|
|
94
|
-
case 502:
|
|
95
|
-
throw new BadGatewayException(statusText, { response });
|
|
96
|
-
case 503:
|
|
97
|
-
throw new ServiceUnavailableException(statusText, { response });
|
|
98
|
-
case 504:
|
|
99
|
-
throw new GatewayTimeoutException(statusText, { response });
|
|
100
|
-
default:
|
|
101
|
-
throw new RequestException(status, statusText, { fatal: false, response });
|
|
102
|
-
}
|
|
103
|
-
}
|
|
110
|
+
throw createExceptionByStatusCode(response);
|
|
104
111
|
};
|
|
105
112
|
}
|
|
106
113
|
export {
|
|
107
114
|
catchException,
|
|
115
|
+
createExceptionByStatusCode,
|
|
108
116
|
throwException,
|
|
109
117
|
validateStatusCode
|
|
110
118
|
};
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/throw-exception.ts","../src/catch-exception.ts","../src/validate-status-code.ts"],"sourcesContent":["import { KeqContext, KeqMiddleware } from 'keq'\nimport { Promisable } from 'type-fest'\n\n\nexport type Check = (ctx: KeqContext) => Promisable<void>\n\nexport function throwException(check: Check): KeqMiddleware {\n return async function throwException(ctx, next) {\n await next()\n\n await check(ctx)\n }\n}\n","import { KeqMiddleware } from 'keq'\nimport { Promisable } from 'type-fest'\n\n\nexport function catchException(handler: (e: unknown) => Promisable<void>): KeqMiddleware {\n return async function catchException(ctx, next) {\n try {\n await next()\n } catch (err) {\n await handler(err)\n }\n }\n}\n","import {\n BadGatewayException,\n BadRequestException,\n ConflictException,\n ForbiddenException,\n GatewayTimeoutException,\n InternalServerErrorException,\n
|
|
1
|
+
{"version":3,"sources":["../src/throw-exception.ts","../src/catch-exception.ts","../src/create-exception-by-status-code.ts","../src/validate-status-code.ts"],"sourcesContent":["import { KeqContext, KeqMiddleware } from 'keq'\nimport { Promisable } from 'type-fest'\n\n\nexport type Check = (ctx: KeqContext) => Promisable<void>\n\nexport function throwException(check: Check): KeqMiddleware {\n return async function throwException(ctx, next) {\n await next()\n\n await check(ctx)\n }\n}\n","import { KeqMiddleware } from 'keq'\nimport { Promisable } from 'type-fest'\n\n\nexport function catchException(handler: (e: unknown) => Promisable<void>): KeqMiddleware {\n return async function catchException(ctx, next) {\n try {\n await next()\n } catch (err) {\n await handler(err)\n }\n }\n}\n","import {\n BadGatewayException,\n BadRequestException,\n ConflictException,\n ForbiddenException,\n GatewayTimeoutException,\n InternalServerErrorException,\n NotAcceptableException,\n NotFoundedException,\n PreconditionFailedException,\n RequestException,\n ServiceUnavailableException,\n UnauthorizedException,\n ImATeapotException,\n MethodNotAllowedException,\n UriTooLongException,\n ContentTooLargeException,\n ProxyAuthenticationRequiredException,\n RequestTimeoutException,\n TooManyRequestsException,\n NotImplementedException,\n UnsupportedMediaTypeException,\n} from 'keq'\n\nexport function createExceptionByStatusCode(response: Response): Error {\n const { status, statusText } = response\n\n // 4xx client errors\n if (status >= 400 && status < 500) {\n switch (status) {\n case 400:\n return new BadRequestException(statusText, { response })\n case 401:\n return new UnauthorizedException(statusText, { response })\n case 403:\n return new ForbiddenException(statusText, { response })\n case 404:\n return new NotFoundedException(statusText, { response })\n case 405:\n return new MethodNotAllowedException(statusText, { response })\n case 406:\n return new NotAcceptableException(statusText, { response })\n case 407:\n return new ProxyAuthenticationRequiredException(statusText, { response })\n case 408:\n return new RequestTimeoutException(statusText, { response })\n case 409:\n return new ConflictException(statusText, { response })\n case 412:\n return new PreconditionFailedException(statusText, { response })\n case 413:\n return new ContentTooLargeException(statusText, { response })\n case 414:\n return new UriTooLongException(statusText, { response })\n case 415:\n return new UnsupportedMediaTypeException(statusText, { response })\n case 418:\n return new ImATeapotException(statusText, { response })\n case 429:\n return new TooManyRequestsException(statusText, { response })\n default:\n // Other 4xx errors, don't retry by default\n return new RequestException(status, statusText, { fatal: true, response })\n }\n }\n\n // 5xx server errors\n if (status >= 500) {\n switch (status) {\n case 500:\n return new InternalServerErrorException(statusText, { response })\n case 501:\n return new NotImplementedException(statusText, { response })\n case 502:\n return new BadGatewayException(statusText, { response })\n case 503:\n return new ServiceUnavailableException(statusText, { response })\n case 504:\n return new GatewayTimeoutException(statusText, { response })\n default:\n // Other 5xx errors, retry by default\n return new RequestException(status, statusText, { fatal: false, response })\n }\n }\n\n return new RequestException(status, statusText, { fatal: false, response })\n}\n","import { KeqMiddleware } from 'keq'\nimport { createExceptionByStatusCode } from './create-exception-by-status-code.js'\n\nexport function validateStatusCode(): KeqMiddleware {\n return async function validateStatusCode(context, next) {\n await next()\n\n const response = context.response\n if (!response) return\n\n const { status } = response\n\n // 2xx success status codes - no error\n if (status >= 200 && status < 300) return\n\n // 3xx redirection status codes - no error (handled by fetch)\n if (status >= 300 && status < 400) return\n\n throw createExceptionByStatusCode(response)\n }\n}\n"],"mappings":";AAMO,SAAS,eAAe,OAA6B;AAC1D,SAAO,eAAeA,gBAAe,KAAK,MAAM;AAC9C,UAAM,KAAK;AAEX,UAAM,MAAM,GAAG;AAAA,EACjB;AACF;;;ACRO,SAAS,eAAe,SAA0D;AACvF,SAAO,eAAeC,gBAAe,KAAK,MAAM;AAC9C,QAAI;AACF,YAAM,KAAK;AAAA,IACb,SAAS,KAAK;AACZ,YAAM,QAAQ,GAAG;AAAA,IACnB;AAAA,EACF;AACF;;;ACZA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEA,SAAS,4BAA4B,UAA2B;AACrE,QAAM,EAAE,QAAQ,WAAW,IAAI;AAG/B,MAAI,UAAU,OAAO,SAAS,KAAK;AACjC,YAAQ,QAAQ;AAAA,MACd,KAAK;AACH,eAAO,IAAI,oBAAoB,YAAY,EAA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EAAE,SAAS,CAAC;AAAA,MAC7D,KAAK;AACH,eAAO,IAAI,oBAAoB,YAAY,EAAE,SAAS,CAAC;AAAA,MACzD,KAAK;AACH,eAAO,IAAI,4BAA4B,YAAY,EAAE,SAAS,CAAC;AAAA,MACjE,KAAK;AACH,eAAO,IAAI,wBAAwB,YAAY,EAAE,SAAS,CAAC;AAAA,MAC7D;AAEE,eAAO,IAAI,iBAAiB,QAAQ,YAAY,EAAE,OAAO,OAAO,SAAS,CAAC;AAAA,IAC9E;AAAA,EACF;AAEA,SAAO,IAAI,iBAAiB,QAAQ,YAAY,EAAE,OAAO,OAAO,SAAS,CAAC;AAC5E;;;ACnFO,SAAS,qBAAoC;AAClD,SAAO,eAAeC,oBAAmB,SAAS,MAAM;AACtD,UAAM,KAAK;AAEX,UAAM,WAAW,QAAQ;AACzB,QAAI,CAAC,SAAU;AAEf,UAAM,EAAE,OAAO,IAAI;AAGnB,QAAI,UAAU,OAAO,SAAS,IAAK;AAGnC,QAAI,UAAU,OAAO,SAAS,IAAK;AAEnC,UAAM,4BAA4B,QAAQ;AAAA,EAC5C;AACF;","names":["throwException","catchException","validateStatusCode"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate-status-code.d.ts","sourceRoot":"","sources":["../src/validate-status-code.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"validate-status-code.d.ts","sourceRoot":"","sources":["../src/validate-status-code.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAA;AAGnC,wBAAgB,kBAAkB,IAAI,aAAa,CAiBlD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keq-request/exception",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.32",
|
|
4
4
|
"description": "Request exception for keq",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"keq",
|
|
@@ -39,14 +39,14 @@
|
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@scalar/openapi-types": "^0.5.2",
|
|
41
41
|
"@types/node": "^20.19.25",
|
|
42
|
-
"@keq-request/cli": "5.0.0-alpha.
|
|
43
|
-
"keq": "5.0.0-alpha.
|
|
42
|
+
"@keq-request/cli": "5.0.0-alpha.32",
|
|
43
|
+
"keq": "5.0.0-alpha.32"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"keq": "^5.0.0-alpha.
|
|
46
|
+
"keq": "^5.0.0-alpha.32"
|
|
47
47
|
},
|
|
48
48
|
"optionalDependencies": {
|
|
49
|
-
"@keq-request/cli": "5.0.0-alpha.
|
|
49
|
+
"@keq-request/cli": "5.0.0-alpha.32"
|
|
50
50
|
},
|
|
51
51
|
"scripts": {
|
|
52
52
|
"build": "tsup",
|