1+ import { stacktrace } from "stacktrace-parser-node" ;
12import { sendEvent } from "../core/http" ;
23import { isClientConnected , isLocalhost } from "../core/is" ;
34import { TraceoError } from "../transport/base" ;
45import { TraceoEvent } from "../transport/events" ;
56import { TraceoIncomingMessage , TraceoServerResponse } from "../transport/http" ;
67import { ErrorMiddlewareOptions } from "../transport/options" ;
7- import { getIp , getProtocol } from "./helpers" ;
8- import { prepareException } from "./parse" ;
8+ import { getIp , getOsPlatform , getProtocol } from "./helpers" ;
99
1010/**
1111 * Base middleware to catch and intercept error across the express app.
@@ -43,7 +43,7 @@ const errorMiddleware = (options: ErrorMiddlewareOptions = {}) => {
4343 }
4444
4545 if ( isToCatch ( req , options ) ) {
46- await handleException ( error , req ) ;
46+ await handleException ( error ) ;
4747 }
4848
4949 next ( error ) ;
@@ -86,7 +86,7 @@ interface Catch {
8686 * }
8787 * ```
8888 *
89- * Usage with optional parameter `shouldBeCatched` as a callback function :
89+ * Usage with `shouldBeCatched`:
9090 * @example
9191 *
9292 * ```
@@ -106,24 +106,40 @@ export const catchException = async (error: any, catchOptions?: Catch) => {
106106 }
107107
108108 if ( isClientConnected ( ) ) {
109- await handleException ( error , undefined ) ;
109+ await handleException ( error ) ;
110110 }
111111
112112 return ;
113113} ;
114114
115- const handleException = async (
116- error : TraceoError ,
117- req ?: TraceoIncomingMessage
118- ) => {
115+ const handleException = async ( error : TraceoError ) => {
119116 try {
120- const event : TraceoEvent = await prepareException ( error , req ) ;
117+ const event : TraceoEvent = await prepareException ( error ) ;
121118 await sendEvent ( event ) ;
122119 } catch ( err ) {
123120 //
124121 }
125122} ;
126123
124+
125+ const prepareException = async (
126+ error : TraceoError
127+ ) : Promise < TraceoEvent > => {
128+ const { stack } = error ;
129+ const platform = getOsPlatform ( ) ;
130+
131+ const { message, name, traces } = await stacktrace . parse ( error ) ;
132+ const event : TraceoEvent = {
133+ type : name ,
134+ message,
135+ traces,
136+ stack : String ( stack ) ,
137+ platform,
138+ } ;
139+
140+ return event ;
141+ } ;
142+
127143export const Middleware = {
128144 errorMiddleware,
129145} ;
0 commit comments