From 19be2e76947bfe5ccea5978d7f076f6950967112 Mon Sep 17 00:00:00 2001 From: g-tejas Date: Wed, 26 Jun 2024 13:01:24 +0800 Subject: [PATCH] feat(errors): revamp business logic error codes --- src/app/modules/core/core.errors.ts | 4 +++- src/app/modules/form/form.errors.ts | 2 +- .../form/public-form/public-form.controller.ts | 17 +++++++++++++++++ .../email-submission.controller.ts | 6 ++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/app/modules/core/core.errors.ts b/src/app/modules/core/core.errors.ts index 995fe73015..dce8fa7739 100644 --- a/src/app/modules/core/core.errors.ts +++ b/src/app/modules/core/core.errors.ts @@ -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) @@ -18,6 +19,7 @@ export class ApplicationError extends Error { this.message = message || 'Something went wrong. Please try again.' this.meta = meta + this.errorCode = errorCode } } diff --git a/src/app/modules/form/form.errors.ts b/src/app/modules/form/form.errors.ts index 5ef8cdc8cc..e2b604b461 100644 --- a/src/app/modules/form/form.errors.ts +++ b/src/app/modules/form/form.errors.ts @@ -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) this.formTitle = formTitle } } diff --git a/src/app/modules/form/public-form/public-form.controller.ts b/src/app/modules/form/public-form/public-form.controller.ts index e30fec669b..dc7a6ba986 100644 --- a/src/app/modules/form/public-form/public-form.controller.ts +++ b/src/app/modules/form/public-form/public-form.controller.ts @@ -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' @@ -96,6 +97,19 @@ export const handleGetPublicForm: ControllerHandler< } const { errorMessage, statusCode } = mapRouteError(error) + if (error?.errorCode) { + tracer.dogstatsd.increment('errorsV2', 1, { + error_code: error.errorCode, + }) + } + + const span = tracer.scope().active() + if (span) { + span.setTag('error', true) + span.setTag('error.message', errorMessage) + span.setTag('error.status_code', error?.errorCode) + } + // Specialized error response for PrivateFormError. // This is to maintain backwards compatibility with the middleware implementation if (error instanceof PrivateFormError) { @@ -105,6 +119,9 @@ export const handleGetPublicForm: ControllerHandler< // showing. isPageFound: true, formTitle: error.formTitle, + ...(error.errorCode !== undefined + ? { error_code: error.errorCode } + : {}), }) } diff --git a/src/app/modules/submission/email-submission/email-submission.controller.ts b/src/app/modules/submission/email-submission/email-submission.controller.ts index 2cc6f73dde..375c89fab2 100644 --- a/src/app/modules/submission/email-submission/email-submission.controller.ts +++ b/src/app/modules/submission/email-submission/email-submission.controller.ts @@ -1,3 +1,4 @@ +import tracer from 'dd-trace' import { ok, okAsync, ResultAsync } from 'neverthrow' import { @@ -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)