Skip to content

Commit

Permalink
feat(errors): revamp business logic error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
g-tejas committed Jun 26, 2024
1 parent 4ceb3d1 commit ce9af29
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/app/modules/core/core.errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ export class ApplicationError extends Error {
* Meta object to be logged by the application logger, if any.
*/
meta?: unknown
errorCode?: number

constructor(message?: string, meta?: unknown) {
constructor(message?: string, meta?: unknown, errorCode?: number) {
super()

Error.captureStackTrace(this, this.constructor)
Expand All @@ -18,6 +19,7 @@ export class ApplicationError extends Error {
this.message = message || 'Something went wrong. Please try again.'

this.meta = meta
this.errorCode = errorCode
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/form/form.errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class PrivateFormError extends ApplicationError {
message = 'If you think this is a mistake, please contact the agency that gave you the form link.',
formTitle: string,
) {
super(message)
super(message, 1001)
this.formTitle = formTitle
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/app/modules/form/public-form/public-form.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpStatusCode } from 'axios'
import { celebrate, Joi, Segments } from 'celebrate'
import tracer from 'dd-trace'
import { StatusCodes } from 'http-status-codes'
import { err, ok, Result } from 'neverthrow'
import { UnreachableCaseError } from 'ts-essentials'
Expand Down Expand Up @@ -96,6 +97,12 @@ export const handleGetPublicForm: ControllerHandler<
}
const { errorMessage, statusCode } = mapRouteError(error)

if (error?.errorCode) {
tracer.dogstatsd.increment('errorsV2', 1, {
error_code: error.errorCode,
})
}

// Specialized error response for PrivateFormError.
// This is to maintain backwards compatibility with the middleware implementation
if (error instanceof PrivateFormError) {
Expand All @@ -105,6 +112,9 @@ export const handleGetPublicForm: ControllerHandler<
// showing.
isPageFound: true,
formTitle: error.formTitle,
...(error.errorCode !== undefined
? { error_code: error.errorCode }
: {}),
})
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import tracer from 'dd-trace'
import { ok, okAsync, ResultAsync } from 'neverthrow'

import {
Expand Down Expand Up @@ -440,6 +441,11 @@ const submitEmailModeForm: ControllerHandler<
},
)
.mapErr((error) => {
if (error?.errorCode) {
tracer.dogstatsd.increment('errorsV2', 1, {
error_code: error.errorCode,
})
}
const { errorMessage, statusCode } = mapRouteError(error)
return res
.status(statusCode)
Expand Down

0 comments on commit ce9af29

Please sign in to comment.