-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c44e1a4
commit f897608
Showing
7 changed files
with
127 additions
and
43 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Notifier } from "@airbrake/node"; | ||
import { isLiveEnv } from "../helpers"; | ||
|
||
export const airbrake = | ||
isLiveEnv() && | ||
process.env.AIRBRAKE_PROJECT_ID && | ||
process.env.AIRBRAKE_PROJECT_KEY | ||
? new Notifier({ | ||
projectId: Number(process.env.AIRBRAKE_PROJECT_ID), | ||
projectKey: process.env.AIRBRAKE_PROJECT_KEY, | ||
environment: process.env.NODE_ENV!, | ||
}) | ||
: undefined; | ||
|
||
/** | ||
* Simple helper function to manually report an error to Airbrake | ||
* To be used when you do not want to throw an error and halt execution | ||
*/ | ||
export const reportError = (report: { error: any; context: object }) => | ||
airbrake ? airbrake.notify(report) : console.log(report); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { ErrorRequestHandler, RequestHandler } from "express"; | ||
import { ServerError } from "./ServerError"; | ||
import { airbrake } from "./airbrake"; | ||
import airbrakeExpress from "@airbrake/node/dist/instrumentation/express"; | ||
|
||
/** | ||
* Sets up Airbrake metrics recording | ||
*/ | ||
export const airbrakeMiddleware: RequestHandler = (_req, _res, next) => { | ||
if (!airbrake) return next(); | ||
|
||
return airbrakeExpress.makeMiddleware(airbrake); | ||
}; | ||
|
||
/** | ||
* Log errors to Airbrake | ||
*/ | ||
export const errorLogger: ErrorRequestHandler = ( | ||
errorObject, | ||
req, | ||
_res, | ||
next, | ||
) => { | ||
if (!airbrake) { | ||
console.log(errorObject); | ||
return next(errorObject); | ||
} | ||
|
||
// Default Airbrake notice for all errors | ||
// See https://github.com/airbrake/airbrake-js/blob/master/packages/node/src/instrumentation/express.ts | ||
const notice = { | ||
error: errorObject, | ||
context: { | ||
userAddr: req.ip, | ||
userAgent: req.headers["user-agent"], | ||
url: req.protocol + "://" + req.headers.host + req.originalUrl, | ||
httpMethod: req.method, | ||
component: "express", | ||
route: req.route?.path?.toString(), | ||
action: req.route?.stack?.[0]?.name, | ||
referer: req.headers?.referer, | ||
message: "Something went wrong", | ||
}, | ||
}; | ||
|
||
// Append additional information for explicitly caught errors | ||
if (errorObject instanceof ServerError) { | ||
notice.context = { | ||
...notice.context, | ||
// ...errorObject.context, | ||
message: errorObject.message, | ||
}; | ||
|
||
// Log original error if provided | ||
if (errorObject.cause) notice.error = errorObject.cause; | ||
} | ||
|
||
// Send notice to Airbrake | ||
airbrake.notify(notice); | ||
|
||
return next({ | ||
...errorObject, | ||
message: errorObject.message.concat(", this error has been logged"), | ||
}); | ||
}; | ||
|
||
export const errorResponder: ErrorRequestHandler = ( | ||
errorObject, | ||
_req, | ||
res, | ||
_next, | ||
) => { | ||
const { status = 500, message = "Something went wrong" } = errorObject; | ||
|
||
res.status(status).send({ | ||
error: message, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters