Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip(api): Airbrake error handling #2819

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions api.planx.uk/airbrake.ts

This file was deleted.

22 changes: 19 additions & 3 deletions api.planx.uk/errors/ServerError.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
export class ServerError extends Error {
/**
* Message passed to user who triggered error
*/
message: string;
/**
* HTTP status code to be returned to user
* @default 500
*/
status: number;
/**
* Original error, to be passed to Airbrake or logged in local dev
*/
cause: unknown | undefined;
/**
* Context obejct passed to Airbrake
* Can hold any key-value data which may prove useful for debugging
*/
context: object | undefined;

constructor({
message,
status,
cause,
context,
}: {
message: string;
status?: number;
cause?: unknown;
context?: object | undefined;
}) {
super(message);
this.message = message;
this.status = status || 500;
if (cause) {
this.cause = cause;
}
if (cause) this.cause = cause;
if (context) this.context = context;

// Set the prototype explicitly.
Object.setPrototypeOf(this, ServerError.prototype);
Expand Down
20 changes: 20 additions & 0 deletions api.planx.uk/errors/airbrake.ts
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 }) =>

Check warning on line 19 in api.planx.uk/errors/airbrake.ts

View workflow job for this annotation

GitHub Actions / Run API Tests

Unexpected any. Specify a different type
airbrake ? airbrake.notify(report) : console.log(report);
78 changes: 78 additions & 0 deletions api.planx.uk/errors/middleware.ts
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,
});
};
19 changes: 2 additions & 17 deletions api.planx.uk/modules/send/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { gql } from "graphql-request";
import airbrake from "../../../airbrake";
import { $api } from "../../../client";
import { ServerError } from "../../../errors";
import { reportError } from "../../../errors/airbrake";

export async function logPaymentStatus({
sessionId,
Expand Down Expand Up @@ -44,22 +45,6 @@ export async function logPaymentStatus({
}
}

// tmp explicit error handling
export function reportError(report: { error: any; context: object }) {
if (airbrake) {
airbrake.notify(report);
return;
}
log(report);
}

// tmp logger
function log(event: object | string) {
if (!process.env.SUPPRESS_LOGS) {
console.log(event);
}
}

// TODO: this would ideally live in planx-client
async function insertPaymentStatus({
flowId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isObject } from "lodash";
import { JSDOM } from "jsdom";
import createDOMPurify from "dompurify";
import { reportError } from "../../../send/utils/helpers";
import { decode } from "he";
import { reportError } from "../../../../errors/airbrake";

// Setup JSDOM and DOMPurify
const window = new JSDOM("").window;
Expand Down
61 changes: 38 additions & 23 deletions api.planx.uk/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import assert from "assert";
import cookieParser from "cookie-parser";
import cookieSession from "cookie-session";
import cors, { CorsOptions } from "cors";
import express, { ErrorRequestHandler } from "express";
import express from "express";
import noir from "pino-noir";
import pinoLogger from "express-pino-logger";
import { Server } from "http";
import passport from "passport";
import helmet from "helmet";
import { ServerError } from "./errors";
import airbrake from "./airbrake";
import { apiLimiter } from "./rateLimit";
import { googleStrategy } from "./modules/auth/strategy/google";
import authRoutes from "./modules/auth/routes";
Expand All @@ -32,6 +30,12 @@ import payRoutes from "./modules/pay/routes";
import sendRoutes from "./modules/send/routes";
import { useSwaggerDocs } from "./docs";
import { Role } from "@opensystemslab/planx-core/types";
import { ServerError } from "./errors";
import {
airbrakeMiddleware,
errorResponder,
errorLogger,
} from "./errors/middleware";

const app = express();

Expand Down Expand Up @@ -134,6 +138,10 @@ app.use(passport.initialize());
app.use(passport.session());
app.use(urlencoded({ extended: true }));

// Setup Airbrake
// Must be before all routes
app.use(airbrakeMiddleware);

// Setup API routes
app.use(adminRoutes);
app.use(analyticsRoutes);
Expand All @@ -151,31 +159,38 @@ app.use(teamRoutes);
app.use(userRoutes);
app.use(webhookRoutes);

const errorHandler: ErrorRequestHandler = (errorObject, _req, res, _next) => {
const { status = 500, message = "Something went wrong" } = (() => {
if (
airbrake &&
(errorObject instanceof Error || errorObject instanceof ServerError)
) {
airbrake.notify(errorObject);
return {
...errorObject,
message: errorObject.message.concat(", this error has been logged"),
};
} else {
console.log(errorObject);
return errorObject;
}
})();
app.get("/next-error", (_req, _res, next) => {
const error = new Error("Error passed to next() in /next-error");
next(error);
});

res.status(status).send({
error: message,
app.get("/next-server-error", (_req, _res, next) => {
const serverError = new ServerError({
message: "ServerError passed to next() in /next-server-error",
context: {
someNextData: "someNextValue",
},
});
};
next(serverError);
});

app.get("/throw-next-error", (_req, _res, _next) => {
throw new Error("Error passed to next() in /next-error");
});

app.get("/throw-server-error", (_req, _res, _next) => {
throw new ServerError({
message: "ServerError thrown in /next-server-error",
context: {
someThrownData: "someThrownValue",
},
});
});

// Handle any server errors that were passed with next(err)
// Order is significant, this should be the final app.use()
app.use(errorHandler);
app.use(errorLogger);
app.use(errorResponder);

const server = new Server(app);

Expand Down
Loading