Problem
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.
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.
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.
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')
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
}
}
Last modified 1mo ago