Skip to content

Commit

Permalink
fix: send error.statusCode when available if the reply.statusCode is …
Browse files Browse the repository at this point in the history
…200 or undefined
  • Loading branch information
mdoria12 authored and dnlup committed Jul 7, 2023
1 parent 805ca7e commit e06ac12
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ exports.onResponseSize = function (request, reply, payload, next) {
};

exports.onError = function (request, reply, error, done) {
let code = undefined;
/* istanbul ignore next */
const code = reply.statusCode < 400 ? 500 : reply.statusCode;
if (!reply.statusCode || reply.statusCode === 200) {
// reply has 200 statusCode by default with fastify 4 so we need to check and retrieve the status code of the Error object.
// @see https://github.com/fastify/fastify/issues/4501
code = error.statusCode || error.status || 500;
} else {
code = reply.statusCode < 400 ? 500 : reply.statusCode;
}

request.sendCounterMetric(`errors.${code}`);
done();
};

0 comments on commit e06ac12

Please sign in to comment.