Skip to content

Commit

Permalink
chore: rollout usage of error handling
Browse files Browse the repository at this point in the history
Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
  • Loading branch information
james-a-morris committed Nov 26, 2024
1 parent ba78976 commit c3b2187
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 33 deletions.
45 changes: 45 additions & 0 deletions packages/indexer-api/src/error-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
isIndexerError,
isIndexerHTTPError,
StatusCodes,
} from "@repo/error-handling";
import { Request, Response, NextFunction } from "express";
import { isHttpError } from "./express-app";
import { StructError } from "superstruct";

const DEFAULT_STATUS = StatusCodes.BAD_REQUEST;

const errorHandler = (
err: unknown,
req: Request,
res: Response,
_: NextFunction,
): void => {
// At a base level we need to confirm that this isn't a valid
// passthrough - if so ignore
if (isIndexerError(err)) {
// If we have a custom sub-type to specify the error code, use it
// otherwise default to a status 400
const httpStatus = isIndexerHTTPError(err)
? err.httpStatusCode
: DEFAULT_STATUS;
res.status(httpStatus).json(err.toJSON());
} else if (isHttpError(err)) {
res.status(err.status ?? DEFAULT_STATUS).json({
message: err.message,
error: "NavigationError",
});
} else if (err instanceof StructError) {
res.status(StatusCodes.BAD_REQUEST).json({
error: "InputValidationError",
message: err.message,
});
} else if (err instanceof Error) {
res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
error: "UnknownError",
message: err.message,
});
}
};

export default errorHandler;
28 changes: 5 additions & 23 deletions packages/indexer-api/src/express-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import cors from "cors";
import bodyParser from "body-parser";
import type { Request, Response, NextFunction, Express, Router } from "express";
import express from "express";
import errorHandler from "./error-handler";

export class HttpError extends Error {
status?: number;
Expand Down Expand Up @@ -31,33 +32,14 @@ export function ExpressApp(routers: RouterConfigs): Express {
});

app.use(function (_: Request, __: Response, next: NextFunction) {
const error = new HttpError("Not Found");
const error = new HttpError("Route does not exist.");
error["status"] = 404;
next(error);
});

app.use(function (
err: HttpError | Error,
req: Request,
res: Response,
// this needs to be included even if unused, since 4 param call triggers error handler
_: NextFunction,
) {
const request = {
method: req.method,
path: req.path,
body: req.body,
};
let status = 500;
if (isHttpError(err)) {
status = err.status ?? status;
}
res.status(status).json({
message: err.message,
request,
stack: err.stack,
});
});
// Register an error handler as the last part of the
// express pipeline
app.use(errorHandler);

return app;
}
19 changes: 9 additions & 10 deletions packages/indexer-api/src/services/exceptions.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { HttpError } from "../express-app";
import { HttpStatus } from "../model/httpStatus";
import { IndexerHTTPError, StatusCodes } from "@repo/error-handling";

export class DepositNotFoundException extends HttpError {
export class DepositNotFoundException extends IndexerHTTPError {
constructor() {
super("Deposit not found");
this.name = "DepositNotFoundException";
this.status = HttpStatus.NOT_FOUND;
super(
StatusCodes.NOT_FOUND,
DepositNotFoundException.name,
"Deposit not found given the provided constraints",
);
}
}

export class IndexParamOutOfRangeException extends HttpError {
export class IndexParamOutOfRangeException extends IndexerHTTPError {
constructor(message: string) {
super(message);
this.name = "IndexParamOutOfRangeException";
this.status = HttpStatus.BAD_REQUEST;
super(StatusCodes.BAD_REQUEST, IndexParamOutOfRangeException.name, message);
}
}

0 comments on commit c3b2187

Please sign in to comment.