Search…
Problem Types
This library has TypeScript enums and types for the problems that our APIs return.

Handling problems with Apollo Client.

If you're using @island.is/nest/problem in your GraphQL API, the problem will be included as a GraphQL Error extension.
This library includes a utility to handle Problems on the client side:
1
import { useMutation } from '@apollo/client'
2
import {
3
ProblemType,
4
findProblemInApolloError,
5
} from '@island.is/shared/problem'
6
7
// In some component:
8
const [callApi, { error }] = useMutation(MUTATION)
9
const problem = findProblemInApolloError(error) // undefined | Problem
10
11
if (problem && problem.type === ProblemType.SOMETHING) {
12
// oh no
13
}
Copied!

Custom problem

Client code should use the type attribute as the primary identifier for problems. The other standard attributes are mainly helpful for developer experience and logs.
If you are ever tempted to create business logic around the title or details of a problem, or need extra metadata in problems, then consider to create a new custom problem.
First, add your new type to the ProblemType enum, with a docs.devland.is URL:
1
export enum ProblemType {
2
// [snip]
3
NEW_PROBLEM = 'https://docs.devland.is/reference/problems/new-problem',
4
}
Copied!
Then add an interface for your problem in problems.ts.
1
export interface NewProblem extends BaseProblem {
2
type: ProblemType.NEW_PROBLEM
3
extraAttribute: string
4
}
Copied!
Add your problem interface to the Problem union type.
Finally, add the new problem type to our docs. Make sure the documentation URL matches your problem type URL.
Check out the @island.is/nest/problem docs on how to return your new custom problem.

Running unit tests

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