Skip to content

Latest commit

 

History

History
94 lines (82 loc) · 2.67 KB

custom-error-instance.function.md

File metadata and controls

94 lines (82 loc) · 2.67 KB

customErrorInstance - Function

If the customErrorInstance option is provided, we will log the original error, but we will throw a new exception with the provided instance of the error, and the original error will be logged.


Suppose you have an ExpressJs API, a global handler for specific exceptions and a fetch user function, this method needs to fire a specific exception, so that your API can handle the return correctly, but your service fires a different type of exception.

Use case

import { Request, Response } from 'express'
import { NotFoundException } from './errors'
import { userService } from './user-service'
import { loggerService } from './logger-service'

async function getUserById(req: Request, res: Response): Promise<void> {
  try {
    const id = Number(req.params.id)
    const user = await userService.getById(id)
    res.json(user)
  } catch (error) {
    loggerService.error(error)
    throw new NotFoundException('User not found')
  }
} 

So to maintain the expected behavior, we provide the option to customize the error instance with customErrorException.

Usage

import { Request, Response } from 'express'
import { catchException } from '@sabbath/logger';
import { NotFoundException } from './errors';
import { userService } from './user-service';

const getUserById = catchException(
  async function handleGetUserById(req: Request, res: Response): Promise<void> {
    const id = Number(req.params.id)
    const user = await userService.getById(id)
    res.json(user)
  },
  {
    kind: 'Application',
    customErrorInstance: new NotFoundException('User not found')
  }
)
import { Request, Response } from 'express'
import { catchException, CatchExceptionOptions } from '@sabbath/logger';
import { NotFoundException } from './errors';
import { userService } from './user-service';

const options: CatchExceptionOptions = {  
  kind: 'Application',
  customErrorInstance: new NotFoundException('User not found')  
}

async function handleGetUserById(req: Request, res: Response): Promise<void> {
  const id = Number(req.params.id)
  const user = await userService.getById(id)
  res.json(user)
}

const getUserById = catchException(handleGetUserById, options)

Log output

[ERROR] - {
  "uid": "",
  "timestamp": "2023-09-12T22:45:13.468Z",
  "trigger": {
    "name": "handleGetUserById",
    "method": "FUNCTIONAL"
  },
  "params": ["c4baf266-13c9-4d6e-93a7-ff8dccde0905"],
  "error": {
    "name": "UserServiceError",
    "message": "User is blocked",
    "stack": {ErrorStack},
    "kind": "Application"
  }
}

API output

{
  "statusCode": 404,
  "error": "Not Found",
  "message": "User not found."
}