18 Supporting the unhappy path: Error handling with problem+json

 

This chapter covers

  • Finding and categorizing API errors
  • The error-handling format from the OAS tools library
  • The problem+json format
  • Adding error responses to the PetSitter OpenAPI definition

As we have designed and implemented the PetSitter API, we’ve mostly looked at the happy path, which is when everything works according to plan and things are 200 OK. Obviously we want the interactions between our API and its users to be on this path as often as possible, but we cannot always guarantee that. In this chapter we’ll look at the ways things can go wrong and how to handle those situations.

The OpenAPI definition of an API is a contract that both sides, client and server, have to follow. If you look outside the field of technology and into contracts as legal documents, you’ll notice that they don’t just describe the happy path. In fact, the greater part of the legalese in the document usually describes all the potential problems and how to mitigate them. It’s when things go wrong that contracts are the most relevant. Error handling is equally important.

The same process that developers use to collaborate on the happy path, which includes designing schemas and API operations, can and should also guide their approach toward error handling. Each developer can bring their perspective to the table (and so can non-developers involved in the API design process).

18.1 The problem

18.2 Error categories

18.2.1 Finding unhappy paths

18.2.2 Common error patterns

18.3 Requirements for error responses

18.4 The OAS tools format

18.5 The problem+json format

18.6 Adding error responses to OpenAPI

18.6.1 Creating error schemas

18.6.2 Adding errors to operations

18.7 Error-handling guidance

18.7.1 Frontend development

18.7.2 Backend development