Programming API
HttpProblems Helper
Zuplo encourages developers to build APIs with standard and actionable error messages. While any error format is easily supported, we default and encorage developers to adopt the Problem Details for HTTP APIs proposed standard format.
Developers can use the built-in HttpProblems
helper that is included with
Zuplo to easily build standard error messages in custom policies.
For example, using the helper to return an unauthorized error on a custom authentication policy can be done as follows.
This will produce and error response in the standard format. Notice that trace information is included automatically. This makes it easy for users to report problems that can be searched in logs.
The HttpProblems
helper supports most every
HTTP status code.
Some additional examples are shown.
Overriding Property Values#
Each method on the HttpProblems
object supports overriding the default values
of the problem response with custom values. The most common reason for this is
for setting the detail
value to something helpful for the end-user.
Other properties like status
and title
can also be overridden, but make sure
to do so within the rules of the spec.
Note
The most important thing to remember about problem details is that every
instance of a particular error should return the same value for title
. Details
about a specific error should go in the detail
property.
An example of how to correctly use the title
and detail
properties can be
demonstrated with an error that tells the user they provided an unexpected value
for a query parameter called take
that implements pagination. In this case,
the title
is always the same, but the detail
value changes to provide the
user with more detail about the error.
You can see how each of these cases help the user understand the problem, but
still provide the same title
.
Additional Properties#
It can sometimes be helpful to specify additional properties on the problem response. The problem specification requires a few specific fields, but allows for any additions as needed. For example, if we wanted to return an error to a user who was above their quote on creating widgets the error might look like this.
Custom Headers#
At times it can be useful to send custom headers with the error response. This can be done as shown below.
If you want to set headers without any overrides just pass undefined
to the
third argument.