@@ -10,6 +10,7 @@ import { APIToolkitConfig, ClientMetadata, Payload } from '../types.js'
1010import config from '@adonisjs/core/services/config'
1111import { observeAxiosGlobal , observeAxiosNotWebContext } from '../axios.js'
1212import { AxiosStatic } from 'axios'
13+ import { reportError } from '../reportErrors.js'
1314
1415const defaultConfig = {
1516 rootURL : 'https://app.apitoolkit.io' ,
@@ -146,6 +147,10 @@ export default class APIToolkitMiddleware {
146147 this . #config = config
147148 }
148149 public async handle ( { request, response } : HttpContext , next : NextFn ) {
150+ if ( this . #config?. debug ) {
151+ console . log ( 'APIToolkit: adonisjs middleware called' )
152+ }
153+
149154 if ( this . #config?. disable || ! this . #project_id) {
150155 await next ( )
151156 return
@@ -163,43 +168,49 @@ export default class APIToolkitMiddleware {
163168 }
164169 }
165170 const reqBody = this . getSafeBody ( request . body ( ) )
166- await next ( )
167- if ( this . #config?. debug ) {
168- console . log ( 'APIToolkit: adonisjs middleware called' )
169- }
170- const respBody = this . getSafeBody ( response . getBody ( ) )
171- const errors = ctx ?. apitoolkitData . errors || [ ]
172- if ( this . #project_id) {
173- const payload = buildPayload ( {
174- start_time,
175- reqBody,
176- respBody,
177- requestHeaders : request . headers ( ) ,
178- responseHeaders : response . getHeaders ( ) ,
179- reqParams : request . params ( ) ,
180- status_code : response . response . statusCode ,
181- raw_url : request . url ( true ) ,
182- url_path : request . ctx ?. route ?. pattern || '' ,
183- reqQuery : request . qs ( ) ,
184- method : request . method ( ) ,
185- host : request . hostname ( ) || '' ,
186- redactHeaderLists : this . #config?. redactHeaders ?? [ ] ,
187- redactRequestBody : this . #config?. redactRequestBody ?? [ ] ,
188- redactResponseBody : this . #config?. redactResponseBody ?? [ ] ,
189- errors,
190- sdk_type : 'JsAdonis' ,
191- service_version : this . #config?. serviceVersion ,
192- tags : this . #config?. tags ?? [ ] ,
193- msg_id,
194- parent_id : undefined ,
195- project_id : this . #project_id,
196- } )
171+ let serverError = null
172+ try {
173+ await next ( )
174+ } catch ( error ) {
175+ serverError = error
176+ reportError ( serverError )
177+ throw error
178+ } finally {
179+ const respBody = this . getSafeBody ( response . getBody ( ) )
180+ const errors = ctx ?. apitoolkitData . errors || [ ]
181+ const statusCode = serverError !== null ? 500 : response . response . statusCode
182+ if ( this . #project_id) {
183+ const payload = buildPayload ( {
184+ start_time,
185+ reqBody,
186+ respBody,
187+ requestHeaders : request . headers ( ) ,
188+ responseHeaders : response . getHeaders ( ) ,
189+ reqParams : request . params ( ) ,
190+ status_code : statusCode ,
191+ raw_url : request . url ( true ) ,
192+ url_path : request . ctx ?. route ?. pattern || '' ,
193+ reqQuery : request . qs ( ) ,
194+ method : request . method ( ) ,
195+ host : request . hostname ( ) || '' ,
196+ redactHeaderLists : this . #config?. redactHeaders ?? [ ] ,
197+ redactRequestBody : this . #config?. redactRequestBody ?? [ ] ,
198+ redactResponseBody : this . #config?. redactResponseBody ?? [ ] ,
199+ errors,
200+ sdk_type : 'JsAdonis' ,
201+ service_version : this . #config?. serviceVersion ,
202+ tags : this . #config?. tags ?? [ ] ,
203+ msg_id,
204+ parent_id : undefined ,
205+ project_id : this . #project_id,
206+ } )
197207
198- if ( this . #config?. debug ) {
199- console . log ( 'APIToolkit: publish prepared payload ' )
200- console . dir ( payload )
208+ if ( this . #config?. debug ) {
209+ console . log ( 'APIToolkit: publish prepared payload ' )
210+ console . dir ( payload )
211+ }
212+ client . publishMessage ( payload )
201213 }
202- client . publishMessage ( payload )
203214 }
204215 }
205216 getSafeBody ( rqb : any ) : string {
0 commit comments