Skip to content

Commit 5f40b77

Browse files
committed
refactor: remove legacy error handling code and implement new structured error handling
- Deleted old error handler implementations from Express and H3 drivers. - Introduced a new error handling module in the common package to standardize error responses. - Updated Express and H3 drivers to use the new error handling logic. - Added logging for unhandled errors and structured error payloads. - Updated tests to cover new error handling behavior and ensure compatibility.
1 parent e77d7a8 commit 5f40b77

21 files changed

Lines changed: 483 additions & 323 deletions

File tree

create-arkstack/cleaner.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { join } from 'node:path'
55

66
const filesToPatch = [
77
'src/core/app.ts',
8-
'src/core/utils/request-handlers.ts',
98
]
109

1110
for (const file of filesToPatch) {

create-arkstack/src/actions.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ export default class {
241241
'src/core/app.ts',
242242
'src/core/router.ts',
243243
'src/core/bootstrap.ts',
244-
'src/core/utils/request-handlers.ts',
245244
]
246245

247246
for (const file of filesToPatch) {

create-arkstack/tests/lean-profile.test.ts

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('makeLeanProfile', () => {
2020

2121
await mkdir(join(location, 'src/app/http/controllers'), { recursive: true })
2222
await mkdir(join(location, 'src/app/http/resources'), { recursive: true })
23-
await mkdir(join(location, 'src/core/utils'), { recursive: true })
23+
await mkdir(join(location, 'src/core'), { recursive: true })
2424
await mkdir(join(location, 'src/routes'), { recursive: true })
2525
await mkdir(join(location, 'prisma/migrations'), { recursive: true })
2626

@@ -46,29 +46,6 @@ describe('makeLeanProfile', () => {
4646
].join('\n'),
4747
)
4848

49-
await writeFile(
50-
join(location, 'src/core/utils/request-handlers.ts'),
51-
[
52-
'import { ModelNotFoundException } from \'arkormx\'',
53-
'',
54-
'export const ErrorHandler = (cause: unknown) => {',
55-
' const error: Record<string, any> = {}',
56-
'',
57-
' if (cause instanceof ModelNotFoundException) {',
58-
' error.code = 404',
59-
' error.message = `${cause.getModelName()} not found!`',
60-
' }',
61-
'',
62-
' if (!(err instanceof ValidationException) &&\n !(err instanceof ModelNotFoundException)) {',
63-
' error.stack = (cause as Error).stack',
64-
' }',
65-
'',
66-
' return error',
67-
'}',
68-
'',
69-
].join('\n'),
70-
)
71-
7249
await writeFile(
7350
join(location, 'src/core/router.ts'),
7451
[
@@ -144,10 +121,6 @@ describe('makeLeanProfile', () => {
144121
const appContent = await readFile(join(location, 'src/core/app.ts'), 'utf-8')
145122
expect(appContent).not.toContain('import { ModelNotFoundException } from \'arkormx\'')
146123

147-
const handlersContent = await readFile(join(location, 'src/core/utils/request-handlers.ts'), 'utf-8')
148-
expect(handlersContent).not.toContain('ModelNotFoundException')
149-
expect(handlersContent).not.toContain('not found!')
150-
151124
const routerContent = await readFile(join(location, 'src/core/router.ts'), 'utf-8')
152125
expect(routerContent).not.toContain('await ClearRouter.group(\'/api\'')
153126
})

express/src/core/app.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { bindGracefulShutdown } from '@arkstack/common'
22
import { Router } from 'src/core/router'
33
import path from 'path'
4-
import ErrorHandler from './utils/request-handlers'
54
import { ExpressDriver } from '@arkstack/driver-express'
65
import { ArkstackKitDriver, ArkstackRouterAwareCore, ArkstackRouterContract, ArkstackRouteListOptions } from '@arkstack/contract'
76
import { type Express, type Handler } from 'express'
@@ -23,7 +22,6 @@ export default class Application implements ArkstackRouterAwareCore<Express, unk
2322
bindRouter: async (runtime) => {
2423
runtime.use(await Router.bind())
2524
},
26-
errorHandler: ErrorHandler,
2725
})
2826

2927
this.app = app ?? this.driver.createApp()

express/src/core/utils/request-handlers.ts

Lines changed: 0 additions & 104 deletions
This file was deleted.

express/tests/app.test.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.

h3/src/core/app.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { ArkstackKitDriver, ArkstackRouterAwareCore, ArkstackRouterContract, Ark
44
import { H3Driver, type H3Middleware } from '@arkstack/driver-h3'
55
import { H3 } from 'h3'
66
import { Router } from 'src/core/router'
7-
import ErrorHandler from './utils/request-handlers'
87

98
export default class Application implements ArkstackRouterAwareCore<H3, unknown> {
109
private app: H3
@@ -19,10 +18,6 @@ export default class Application implements ArkstackRouterAwareCore<H3, unknown>
1918
*/
2019
constructor(app?: H3) {
2120
this.driver = new H3Driver({
22-
createApp: () =>
23-
new H3({
24-
onError: ErrorHandler,
25-
}),
2621
bindRouter: async (runtime) => {
2722
await Router.bind(runtime)
2823
},

h3/src/core/bootstrap.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import Application from 'src/core/app'
2-
import ErrorHandler from './utils/request-handlers'
3-
import { H3 } from 'h3'
42
import { Validator } from 'kanun'
53
import { ValidatorDBDriver } from './utils/drivers/ValidatorDBDriver'
64
import { fileValidatorPlugin } from '@kanun-hq/plugin-file'
@@ -10,8 +8,5 @@ globalThis.str = str
108
Validator.useDatabase(new ValidatorDBDriver())
119
Validator.use(fileValidatorPlugin)
1210

13-
export const h3App = new H3({
14-
onError: ErrorHandler,
15-
})
16-
17-
export const app = new Application(h3App)
11+
export const app = new Application()
12+
export const h3App = app.getAppInstance()

h3/src/core/utils/errors.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import ErrorHandler from './request-handlers'
21
import { HTTPError } from 'h3'
32
import { HttpContext } from 'clear-router/types/h3'
43

@@ -19,13 +18,9 @@ export class RequestError extends HTTPError {
1918
value: T | null | undefined,
2019
message: string,
2120
code: number = 404,
22-
ctx?: HttpContext,
21+
_ctx?: HttpContext,
2322
): asserts value is T {
2423
if (!value) {
25-
if (ctx) {
26-
return ErrorHandler(new RequestError(message, code), ctx) as never
27-
}
28-
2924
throw new RequestError(message, code)
3025
}
3126
}
@@ -42,13 +37,9 @@ export class RequestError extends HTTPError {
4237
boolean: T,
4338
message: string,
4439
code?: number,
45-
ctx?: HttpContext,
40+
_ctx?: HttpContext,
4641
): asserts boolean is T {
4742
if (boolean) {
48-
if (ctx) {
49-
return ErrorHandler(new RequestError(message, code), ctx) as never
50-
}
51-
5243
throw new RequestError(message, code)
5344
}
5445
}

0 commit comments

Comments
 (0)