@eggjs/onerror 3.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.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +67 -0
  3. package/dist/commonjs/agent.d.ts +6 -0
  4. package/dist/commonjs/agent.js +16 -0
  5. package/dist/commonjs/app.d.ts +12 -0
  6. package/dist/commonjs/app.js +150 -0
  7. package/dist/commonjs/config/config.default.d.ts +27 -0
  8. package/dist/commonjs/config/config.default.js +15 -0
  9. package/dist/commonjs/index.d.ts +1 -0
  10. package/dist/commonjs/index.js +4 -0
  11. package/dist/commonjs/lib/error_view.d.ts +154 -0
  12. package/dist/commonjs/lib/error_view.js +248 -0
  13. package/dist/commonjs/lib/onerror_page.mustache.html +761 -0
  14. package/dist/commonjs/lib/utils.d.ts +10 -0
  15. package/dist/commonjs/lib/utils.js +53 -0
  16. package/dist/commonjs/package.json +3 -0
  17. package/dist/commonjs/types.d.ts +7 -0
  18. package/dist/commonjs/types.js +3 -0
  19. package/dist/esm/agent.d.ts +6 -0
  20. package/dist/esm/agent.js +13 -0
  21. package/dist/esm/app.d.ts +12 -0
  22. package/dist/esm/app.js +144 -0
  23. package/dist/esm/config/config.default.d.ts +27 -0
  24. package/dist/esm/config/config.default.js +10 -0
  25. package/dist/esm/index.d.ts +1 -0
  26. package/dist/esm/index.js +2 -0
  27. package/dist/esm/lib/error_view.d.ts +154 -0
  28. package/dist/esm/lib/error_view.js +241 -0
  29. package/dist/esm/lib/onerror_page.mustache.html +761 -0
  30. package/dist/esm/lib/utils.d.ts +10 -0
  31. package/dist/esm/lib/utils.js +43 -0
  32. package/dist/esm/package.json +3 -0
  33. package/dist/esm/types.d.ts +7 -0
  34. package/dist/esm/types.js +2 -0
  35. package/dist/package.json +4 -0
  36. package/package.json +93 -0
  37. package/src/agent.ts +12 -0
  38. package/src/app.ts +160 -0
  39. package/src/config/config.default.ts +34 -0
  40. package/src/index.ts +1 -0
  41. package/src/lib/error_view.ts +281 -0
  42. package/src/lib/onerror_page.mustache.html +761 -0
  43. package/src/lib/utils.ts +47 -0
  44. package/src/types.ts +12 -0
  45. package/src/typings/index.d.ts +4 -0
@@ -0,0 +1,47 @@
1
+ import path from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+ import type { Context, EggCore } from '@eggjs/core';
4
+ import type { OnerrorError } from 'koa-onerror';
5
+
6
+ export function detectErrorMessage(ctx: Context, err: OnerrorError) {
7
+ // detect json parse error
8
+ if (err.status === 400 &&
9
+ err.name === 'SyntaxError' &&
10
+ ctx.request.is('application/json', 'application/vnd.api+json', 'application/csp-report')) {
11
+ return 'Problems parsing JSON';
12
+ }
13
+ return err.message;
14
+ }
15
+
16
+ export function detectStatus(err: OnerrorError) {
17
+ // detect status
18
+ let status = err.status || 500;
19
+ if (status < 200) {
20
+ // invalid status consider as 500, like urllib will return -1 status
21
+ status = 500;
22
+ }
23
+ return status;
24
+ }
25
+
26
+ export function accepts(ctx: Context) {
27
+ if (ctx.acceptJSON) return 'json';
28
+ if (ctx.acceptJSONP) return 'js';
29
+ return 'html';
30
+ }
31
+
32
+ export function isProd(app: EggCore) {
33
+ return app.config.env !== 'local' && app.config.env !== 'unittest';
34
+ }
35
+
36
+ /**
37
+ * Get the source directory name
38
+ */
39
+ export function getSourceDirname() {
40
+ if (typeof __dirname === 'string') {
41
+ return path.dirname(__dirname);
42
+ }
43
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
44
+ // @ts-ignore
45
+ const __filename = fileURLToPath(import.meta.url);
46
+ return path.dirname(path.dirname(__filename));
47
+ }
package/src/types.ts ADDED
@@ -0,0 +1,12 @@
1
+ import type {
2
+ OnerrorConfig,
3
+ } from './config/config.default.js';
4
+
5
+ export type { OnerrorConfig };
6
+
7
+ declare module '@eggjs/core' {
8
+ // add EggAppConfig overrides types
9
+ interface EggAppConfig {
10
+ onerror: OnerrorConfig;
11
+ }
12
+ }
@@ -0,0 +1,4 @@
1
+ // make sure to import egg typings and let typescript know about it
2
+ // @see https://github.com/whxaxes/blog/issues/11
3
+ // and https://www.typescriptlang.org/docs/handbook/declaration-merging.html
4
+ import 'egg';