-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: rollout usage of error handling
Signed-off-by: james-a-morris <jaamorris@cs.stonybrook.edu>
- Loading branch information
1 parent
ba78976
commit c3b2187
Showing
3 changed files
with
59 additions
and
33 deletions.
There are no files selected for viewing
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,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; |
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 |
---|---|---|
@@ -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); | ||
} | ||
} |