Search
⌃K

Problem

This library includes a Nest Module that implements the Problem Details for HTTP APIs spec.

Usage

Import ProblemModule in your root NestJS module.
import { Module } from '@nestjs/common'
import { ProblemModule } from '@island.is/nest/problem'
@Module({
imports: [ProblemModule],
})
export class AppModule {}
It will automatically filter all thrown errors and convert them to Problem Responses.

GraphQL

For unhandled errors thrown in a GraphQL context, this module will enrich them with a "problem" extension.
If the error already has a problem attribute, then that will be used.
Our Enhanced Fetch client automatically parses problem responses and includes a problem attribute in thrown errors. This means that problems from our REST services are automatically forwarded to the UI.
For expected problems, consider following GraphQL best practices and model them in the GraphQL schema.

Manual problem responses

The easiest way to manually return a problem response is to throw ProblemError or one of its subclasses like this:
import { ProblemError } from '@island.is/nest/problem'
import { ProblemType } from '@island.is/shared/problem'
throw new ProblemError({
type: ProblemType.HTTP_NOT_FOUND,
title: 'Not found',
})
The following sub-classes are available for your convenience:
  • new ValidationFailed(fields) - accepts a record of validation issues.

Custom problems

If none of the existing problem types suits your needs, consider creating a new one.
When you've defined the problem type, you can return it like this:
import { ProblemError } from '@island.is/nest/problem'
import { ProblemType } from '@island.is/shared/problem'
throw new ProblemError({
type: ProblemType.NEW_PROBLEM,
title: 'Some new problem occurred',
extraAttribute: 'example',
})
Optionally, you can create a ProblemError subclass like this:
import { ProblemType } from '@island.is/shared/problem'
import { ProblemError } from './ProblemError'
export class NewProblem extends ProblemError {
constructor(extraAttribute: string) {
super({
type: ProblemType.NEW_PROBLEM,
title: 'Some new problem',
status: 400,
extraAttribute,
})
}
}
// Later:
throw new NewProblem('extra')

No Content response

The API Design Guide describes how REST endpoints using resource IDs, i.e. /delegation/:delegationId, should use 204 No Content response instead of 404 Not Found response when no resource is found.
This module includes a NoContentException class can be thrown to trigger a 204 No Content response:
export class DelegationService {
async findOne(delegationId: string) {
const delegation = await this.delegationModel.findOne({
where: {
id: delegationId,
},
})
if (!delegation) {
throw new NoContentException()
}
return delegation
}
}
Make sure you have included the ProblemModule in your root module. See usage

Running unit tests

Run nx test nest-problem to execute the unit tests via Jest.
Last modified 1mo ago