Skip to content

Commit 573a591

Browse files
committed
refactor(exception_handler): use ctx.logger
1 parent 2e5718b commit 573a591

File tree

2 files changed

+29
-43
lines changed

2 files changed

+29
-43
lines changed

src/exception_handler.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
import is from '@sindresorhus/is'
11-
import type { Logger } from '@adonisjs/logger'
1211
import type { Level } from '@adonisjs/logger/types'
1312

1413
import { parseRange } from './helpers.js'
@@ -133,8 +132,6 @@ export class ExceptionHandler {
133132
*/
134133
protected ignoreCodes: string[] = []
135134

136-
constructor(protected logger: Logger) {}
137-
138135
/**
139136
* Expands status pages
140137
*/
@@ -253,7 +250,7 @@ export class ExceptionHandler {
253250
* Log the error using the logger
254251
*/
255252
const level = this.getErrorLogLevel(httpError)
256-
this.logger.log(
253+
ctx.logger.log(
257254
level,
258255
{
259256
...(level === 'error' || level === 'fatal' ? { err: httpError } : {}),

tests/exceptions/exception_handler.spec.ts

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ import { StatusPageRange, StatusPageRenderer } from '../../src/types/server.js'
1818

1919
test.group('Exception handler | handle', () => {
2020
test('handle error by pretty printing it using youch', async ({ assert }) => {
21-
const logger = new LoggerFactory().create()
22-
const exceptionHandler = new ExceptionHandler(logger)
21+
const exceptionHandler = new ExceptionHandler()
2322
const ctx = new HttpContextFactory().create()
2423

2524
const error = new Error('Something went wrong')
@@ -30,8 +29,7 @@ test.group('Exception handler | handle', () => {
3029
})
3130

3231
test('pretty error as JSON when request accepts JSON', async ({ assert }) => {
33-
const logger = new LoggerFactory().create()
34-
const exceptionHandler = new ExceptionHandler(logger)
32+
const exceptionHandler = new ExceptionHandler()
3533
const ctx = new HttpContextFactory().create()
3634

3735
ctx.request.request.headers['accept'] = 'application/json'
@@ -45,8 +43,7 @@ test.group('Exception handler | handle', () => {
4543
})
4644

4745
test('pretty error as JSON when request accepts JSONAPI', async ({ assert }) => {
48-
const logger = new LoggerFactory().create()
49-
const exceptionHandler = new ExceptionHandler(logger)
46+
const exceptionHandler = new ExceptionHandler()
5047
const ctx = new HttpContextFactory().create()
5148

5249
ctx.request.request.headers['accept'] = 'application/vnd.api+json'
@@ -64,8 +61,7 @@ test.group('Exception handler | handle', () => {
6461
protected debug: boolean = false
6562
}
6663

67-
const logger = new LoggerFactory().create()
68-
const exceptionHandler = new AppExceptionHandler(logger)
64+
const exceptionHandler = new AppExceptionHandler()
6965
const ctx = new HttpContextFactory().create()
7066

7167
const error = new Error('Something went wrong')
@@ -82,8 +78,7 @@ test.group('Exception handler | handle', () => {
8278
protected debug: boolean = false
8379
}
8480

85-
const logger = new LoggerFactory().create()
86-
const exceptionHandler = new AppExceptionHandler(logger)
81+
const exceptionHandler = new AppExceptionHandler()
8782
const ctx = new HttpContextFactory().create()
8883
ctx.request.request.headers['accept'] = 'application/json'
8984

@@ -101,8 +96,7 @@ test.group('Exception handler | handle', () => {
10196
protected debug: boolean = false
10297
}
10398

104-
const logger = new LoggerFactory().create()
105-
const exceptionHandler = new AppExceptionHandler(logger)
99+
const exceptionHandler = new AppExceptionHandler()
106100
const ctx = new HttpContextFactory().create()
107101
ctx.request.request.headers['accept'] = 'application/vnd.api+json'
108102

@@ -122,8 +116,7 @@ test.group('Exception handler | handle', () => {
122116
})
123117

124118
test('use error status code', async ({ assert }) => {
125-
const logger = new LoggerFactory().create()
126-
const exceptionHandler = new ExceptionHandler(logger)
119+
const exceptionHandler = new ExceptionHandler()
127120
const ctx = new HttpContextFactory().create()
128121

129122
const error = new Exception('Something went wrong', { status: 401 })
@@ -133,8 +126,7 @@ test.group('Exception handler | handle', () => {
133126
})
134127

135128
test('render error using the error handle method', async ({ assert }) => {
136-
const logger = new LoggerFactory().create()
137-
const exceptionHandler = new ExceptionHandler(logger)
129+
const exceptionHandler = new ExceptionHandler()
138130
const ctx = new HttpContextFactory().create()
139131

140132
class MyError extends Exception {
@@ -160,8 +152,7 @@ test.group('Exception handler | handle', () => {
160152
}
161153
}
162154

163-
const logger = new LoggerFactory().create()
164-
const exceptionHandler = new AppExceptionHandler(logger)
155+
const exceptionHandler = new AppExceptionHandler()
165156
const ctx = new HttpContextFactory().create()
166157

167158
class MyError extends Exception {}
@@ -183,8 +174,7 @@ test.group('Exception handler | handle', () => {
183174
}
184175
}
185176

186-
const logger = new LoggerFactory().create()
187-
const exceptionHandler = new AppExceptionHandler(logger)
177+
const exceptionHandler = new AppExceptionHandler()
188178
const ctx = new HttpContextFactory().create()
189179

190180
class MyError extends Exception {
@@ -201,8 +191,7 @@ test.group('Exception handler | handle', () => {
201191
})
202192

203193
test('handle literal values raised as exception', async ({ assert }) => {
204-
const logger = new LoggerFactory().create()
205-
const exceptionHandler = new ExceptionHandler(logger)
194+
const exceptionHandler = new ExceptionHandler()
206195
const ctx = new HttpContextFactory().create()
207196

208197
ctx.request.request.headers['accept'] = 'application/json'
@@ -219,8 +208,8 @@ test.group('Exception handler | report', () => {
219208
test('report error using logger', async ({ assert }) => {
220209
const logs: string[] = []
221210
const logger = new LoggerFactory().pushLogsTo(logs).merge({ enabled: true }).create()
222-
const exceptionHandler = new ExceptionHandler(logger)
223-
const ctx = new HttpContextFactory().create()
211+
const exceptionHandler = new ExceptionHandler()
212+
const ctx = new HttpContextFactory().merge({ logger }).create()
224213

225214
const error = new Error('Something went wrong')
226215
await exceptionHandler.report(error, ctx)
@@ -239,8 +228,8 @@ test.group('Exception handler | report', () => {
239228
test('report errors with status code in 400 range as a warning', async ({ assert }) => {
240229
const logs: string[] = []
241230
const logger = new LoggerFactory().pushLogsTo(logs).merge({ enabled: true }).create()
242-
const exceptionHandler = new ExceptionHandler(logger)
243-
const ctx = new HttpContextFactory().create()
231+
const exceptionHandler = new ExceptionHandler()
232+
const ctx = new HttpContextFactory().merge({ logger }).create()
244233

245234
const error = new Exception('Something went wrong', { status: 410 })
246235
await exceptionHandler.report(error, ctx)
@@ -259,8 +248,8 @@ test.group('Exception handler | report', () => {
259248
test('report errors with status code below 400 as info', async ({ assert }) => {
260249
const logs: string[] = []
261250
const logger = new LoggerFactory().pushLogsTo(logs).merge({ enabled: true }).create()
262-
const exceptionHandler = new ExceptionHandler(logger)
263-
const ctx = new HttpContextFactory().create()
251+
const exceptionHandler = new ExceptionHandler()
252+
const ctx = new HttpContextFactory().merge({ logger }).create()
264253

265254
const error = new Exception('Something went wrong', { status: 302 })
266255
await exceptionHandler.report(error, ctx)
@@ -279,8 +268,8 @@ test.group('Exception handler | report', () => {
279268
test('do not report 400, 422 and 401 error codes', async ({ assert }) => {
280269
const logs: string[] = []
281270
const logger = new LoggerFactory().pushLogsTo(logs).merge({ enabled: true }).create()
282-
const exceptionHandler = new ExceptionHandler(logger)
283-
const ctx = new HttpContextFactory().create()
271+
const exceptionHandler = new ExceptionHandler()
272+
const ctx = new HttpContextFactory().merge({ logger }).create()
284273

285274
await exceptionHandler.report(new Exception('Something went wrong', { status: 400 }), ctx)
286275
await exceptionHandler.report(new Exception('Something went wrong', { status: 401 }), ctx)
@@ -297,8 +286,8 @@ test.group('Exception handler | report', () => {
297286
test('do not report internal exceptions', async ({ assert }) => {
298287
const logs: string[] = []
299288
const logger = new LoggerFactory().pushLogsTo(logs).merge({ enabled: true }).create()
300-
const exceptionHandler = new ExceptionHandler(logger)
301-
const ctx = new HttpContextFactory().create()
289+
const exceptionHandler = new ExceptionHandler()
290+
const ctx = new HttpContextFactory().merge({ logger }).create()
302291

303292
await exceptionHandler.report(new errors.E_CANNOT_LOOKUP_ROUTE(['/']), ctx)
304293
await exceptionHandler.report(new errors.E_HTTP_EXCEPTION(), ctx)
@@ -320,8 +309,8 @@ test.group('Exception handler | report', () => {
320309

321310
const logs: string[] = []
322311
const logger = new LoggerFactory().pushLogsTo(logs).merge({ enabled: true }).create()
323-
const exceptionHandler = new AppExceptionHandler(logger)
324-
const ctx = new HttpContextFactory().create()
312+
const exceptionHandler = new AppExceptionHandler()
313+
const ctx = new HttpContextFactory().merge({ logger }).create()
325314

326315
const error = new Exception('Something went wrong')
327316
await exceptionHandler.report(error, ctx)
@@ -337,7 +326,7 @@ test.group('Exception handler | report', () => {
337326
test('report error using the error report method', async ({ assert }) => {
338327
const logs: string[] = []
339328
const logger = new LoggerFactory().pushLogsTo(logs).merge({ enabled: true }).create()
340-
const exceptionHandler = new ExceptionHandler(logger)
329+
const exceptionHandler = new ExceptionHandler()
341330
const ctx = new HttpContextFactory().create()
342331

343332
class MyError extends Exception {
@@ -367,8 +356,8 @@ test.group('Exception handler | report', () => {
367356

368357
const logs: string[] = []
369358
const logger = new LoggerFactory().pushLogsTo(logs).merge({ enabled: true }).create()
370-
const exceptionHandler = new AppExceptionHandler(logger)
371-
const ctx = new HttpContextFactory().create()
359+
const exceptionHandler = new AppExceptionHandler()
360+
const ctx = new HttpContextFactory().merge({ logger }).create()
372361

373362
new Exception('Something went wrong', { code: 'E_CUSTOM_ERROR' })
374363

@@ -388,8 +377,8 @@ test.group('Exception handler | report', () => {
388377
test('log request id in logs when request id exists', async ({ assert }) => {
389378
const logs: string[] = []
390379
const logger = new LoggerFactory().pushLogsTo(logs).merge({ enabled: true }).create()
391-
const exceptionHandler = new ExceptionHandler(logger)
392-
const ctx = new HttpContextFactory().create()
380+
const exceptionHandler = new ExceptionHandler()
381+
const ctx = new HttpContextFactory().merge({ logger }).create()
393382

394383
ctx.request.request.headers['x-request-id'] = '123'
395384

0 commit comments

Comments
 (0)