fastify-sentry
Advanced tools
Comparing version 1.1.0 to 1.2.1
@@ -8,3 +8,4 @@ "use strict"; | ||
Sentry.init({ | ||
dsn: options.dsn | ||
dsn: options.dsn, | ||
environment: options.environment ? "local" : options.environment | ||
}); | ||
@@ -20,3 +21,3 @@ fastify.setErrorHandler((err, req, reply) => { | ||
options.errorHandler | ||
? options.errorHandler(req, reply) | ||
? options.errorHandler(err, req, reply) | ||
: reply.send({ | ||
@@ -23,0 +24,0 @@ error: 500, |
{ | ||
"name": "fastify-sentry", | ||
"version": "1.1.0", | ||
"description": | ||
"A plugin for attaching the Sentry SDK error handling to Fastify", | ||
"version": "1.2.1", | ||
"description": "A plugin for attaching the Sentry SDK error handling to Fastify", | ||
"main": "index.js", | ||
@@ -17,3 +16,9 @@ "scripts": { | ||
}, | ||
"keywords": ["fastify", "api", "sentry", "error", "handling"], | ||
"keywords": [ | ||
"fastify", | ||
"api", | ||
"sentry", | ||
"error", | ||
"handling" | ||
], | ||
"author": "Alex Papageorgiou", | ||
@@ -31,4 +36,4 @@ "license": "GPL-3.0-or-later", | ||
"fastify": "^1.13.1", | ||
"tap": "^12.1.0" | ||
"tap": "^12.5.2" | ||
} | ||
} |
@@ -21,3 +21,20 @@ # Fastify Sentry Plugin using the Sentry SDK | ||
{ | ||
dsn: "https://00000000000000000000000000000000@sentry.io/0000000" | ||
dsn: "https://00000000000000000000000000000000@sentry.io/0000000", | ||
environment: "local", | ||
errorHandler: (err, request, reply) => { | ||
// You can specify a custom behavior depending on the context of "request", generate a unique identifier etc. | ||
if (request.raw.url === "/") { | ||
reply.send({ | ||
error: 500, | ||
message: 'The main path "/" didn\'t work!', | ||
payload: err | ||
}); | ||
} else { | ||
reply.send({ | ||
error: 501, | ||
message: "Some other path failed!", | ||
payload: err | ||
}); | ||
} | ||
} | ||
}, | ||
@@ -30,5 +47,11 @@ err => { | ||
fastify.get("/", async (request, reply) => { | ||
// Errors in async functions are automatically caught | ||
throw new Error("Oops"); | ||
reply.send({ hello: "world" }); | ||
}); | ||
fastify.get("/other-path", (request, reply) => { | ||
// On the other hand, you need to pass the Error object to "reply.send" for it to be logged as Fastify does not catch errors in synchronous functions! | ||
reply.send(new Error("I did it again!")); | ||
}); | ||
``` | ||
@@ -35,0 +58,0 @@ |
21
test.js
@@ -8,6 +8,6 @@ "use strict"; | ||
// Custom error handler declaration | ||
const errorHandler = (req, reply) => { | ||
reply.send({ | ||
error: req.body.error, | ||
message: req.body.message | ||
const errorHandler = (err, req, reply) => { | ||
reply.status(req.body.error).send({ | ||
message: req.body.message, | ||
e: err.message | ||
}); | ||
@@ -17,6 +17,7 @@ }; | ||
tap.test("fastify sentry error handler exist", test => { | ||
test.plan(3); | ||
test.plan(4); | ||
fastify.register(fastifySentry, { | ||
dsn: "https://00000000000000000000000000000000@sentry.io/0000000", | ||
environment: "test", | ||
errorHandler: errorHandler | ||
@@ -35,7 +36,9 @@ }); | ||
url: "/", | ||
payload: { error: 500, message: "Internal Server Error" } | ||
payload: { error: 503, message: "Internal Server Error" } | ||
}, | ||
(err, res) => { | ||
test.strictEqual(res.statusCode, 500); | ||
test.strictEqual(res.statusMessage, "Internal Server Error"); | ||
(err, { statusCode, payload }) => { | ||
payload = JSON.parse(payload); | ||
test.strictEqual(statusCode, 503); | ||
test.strictEqual(payload.message, "Internal Server Error"); | ||
test.strictEqual(payload.e, "Oops"); | ||
fastify.close(() => { | ||
@@ -42,0 +45,0 @@ test.end(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42151
68
74