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 27, 2024
1 parent c6ba38d commit 6d4f2ad
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
30 changes: 28 additions & 2 deletions src/app/modules/core/core.errors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import tracer from 'dd-trace'

import { statsdClient } from 'src/app/config/datadog-statsd-client'

export const errorStatsdClient = statsdClient.childClient({
prefix: 'formsg.errors.',
})

/**
* A custom base error class that encapsulates the name, message, status code,
* and logging meta string (if any) for the error.
Expand All @@ -7,8 +15,15 @@ export class ApplicationError extends Error {
* Meta object to be logged by the application logger, if any.
*/
meta?: unknown

constructor(message?: string, meta?: unknown) {
code?: number
isError?: boolean

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

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

this.meta = meta
this.code = errorCode
this.isError = isError
}
}

export const setErrorCodes = (error: ApplicationError) => {
const span = tracer.scope().active()
if (span) {
span.setTag('error', error.isError || false)
span.setTag('error.code', error.code)
span.setTag('error.message', error.message)
}
}

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, undefined, 1001, false)
this.formTitle = formTitle
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/app/modules/form/public-form/public-form.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { isMongoError } from '../../../utils/handle-mongo-error'
import { createReqMeta, getRequestIp } from '../../../utils/request'
import { getFormIfPublic } from '../../auth/auth.service'
import * as BillingService from '../../billing/billing.service'
import { errorStatsdClient, setErrorCodes } from '../../core/core.errors'
import { ControllerHandler } from '../../core/core.types'
import {
MYINFO_AUTH_CODE_COOKIE_NAME,
Expand Down Expand Up @@ -96,6 +97,14 @@ export const handleGetPublicForm: ControllerHandler<
}
const { errorMessage, statusCode } = mapRouteError(error)

errorStatsdClient.increment('', 1, {
code: `${error?.code}`,
message: error.message,
})

// Add error code tags to the active span for datadog
setErrorCodes(error)

// Specialized error response for PrivateFormError.
// This is to maintain backwards compatibility with the middleware implementation
if (error instanceof PrivateFormError) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import tracer from 'dd-trace'

Check failure on line 1 in src/app/modules/submission/email-submission/email-submission.controller.ts

View workflow job for this annotation

GitHub Actions / backend_lint

'tracer' is defined but never used

Check failure on line 1 in src/app/modules/submission/email-submission/email-submission.controller.ts

View workflow job for this annotation

GitHub Actions / backend_lint

'tracer' is defined but never used
import { ok, okAsync, ResultAsync } from 'neverthrow'

import {
Expand Down

0 comments on commit 6d4f2ad

Please sign in to comment.