Problem/Motivation

Existing error messages in Drupal REST responses are one dimensional—a string contextualized by an HTTP status code. This is limiting in several ways:

  • It is very difficult to provide adequate depth of insight into an error via a one-line message.
  • Categorization of "sub-types" below the level of status code and above the specific error message is difficult, inhibiting ease of troubleshooting research
  • The response is too basic to facilitate more advanced integration around fault tolerance and error handling UX.

Proposed resolution

RFC 7807: Problem Details provides a description for structured XML or JSON responses that facilitate greater detail in the error messages.

A few thoughts on some of the available properties:

  • type can link to documentation pages on Drupal.org about the error category, driving developers to a landing page to help facilitate research.
  • title can be made high-level and unique by type, to facilitate googling without the kinds of identifiers, URIs, and file paths that are unique to an environment.

  • instance might be used in the future for more targeted knowledge base building. With some coordination, dedicated URLs or sections within the type URL could be linked to for more specific delving.

Example

{
  "type": "<url>",
  "title": "Access denied to content",
  "status": 403,
  "details": "Currently authenticated user lacks permission to view content on this site."
  "instance": "<url>#access-denied-to-content",
}

Furthermore, problem-specific extensions allow additional properties to be added. There are many future enhancements that might be considered from there.

Remaining tasks

  1. Decide from a product standpoint if this is a good direction for the Drupal REST implementation
  2. Create a facility to support message output in the application/problem+json or application/problem+xml format.
  3. Perform an initial survey of REST error messages to determine categories.
  4. For each error category, find or prepare a documentation page to server as the type URL.
  5. Convert errors thrown in the course of REST responses to be handled by the new error response mechanism.

Out-of-initial-scope

  • Handling multiple problems per error response
  • Coordinating documentation for error "instance" property
  • Adding flags to tailor the error response based on maintenance mode or other site states

User interface changes

REST error responses will change in structure and Content Type. Code which parses and uses the existing structure will break, though backwards compatibility might be preserved by using existing properties as "extensions" per the RFC.

API changes

New representation for errors will likely result in API additions. Changes in error handling might result in API changes to add additional parameters to some exceptions.

Data model changes

None.

Original report

Problem/Motivation

As was discussed in #1865594: Better REST error messages in case of exceptions, we want to respond with clearer error messages. There seem to be two different approaches which are gaining traction, api-problem from the IETF and vnd.error from the HAL community.

Proposed resolution

Decide which to use by default and implement it in a way that makes it easy to change.

CommentFileSizeAuthor
#23 errors.txt1.37 KBdawehner
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

moshe weitzman’s picture

Any new inclination about how to proceed here?

klausi’s picture

Crell's new library might be a candidate for this? https://github.com/Crell/ApiProblem

Crell’s picture

I wasn't really thinking of this issue when I wrote it, but yes, that would make using api-problem dead simple. (I wanted to use it for a project at work, and just to see if I could do it.)

Crell’s picture

Proposal: Let's use ApiProblem, including the library linked above, and call it a day. Yay/nay?

fubhy’s picture

Issue summary: View changes

I just ran into a situation where an actual ApiProblem implementation including a bridge to symfony validator would've been really helpful. It should be easy to respond to a request with a list of validation violations.

Edit: The situation in which I needed this (although it might be obvious) was for implementing node entity forms in a React frontend.

Crell’s picture

ApiProblem is good, but the spec is still not finalized. It's in draft 7 now, I think. I just updated my library for it a few weeks ago. :-)

I'm not against using it, but I want to be cautious about API stability. I'm not sure if we want to lock ourselves into a draft spec that may change for the next however many years of D8's life.

That said, a contrib module that tosses ApiProblem in place would be quite simple to write. I did so for a Symfony project last month and it took less than a day. That may be the safer option from an API stability standpoint.

Crell’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Active » Postponed

Discussed with Klausi in IRC. We'll go with a contrib module for now. Once ApiProblem settles down as a spec we can look at moving it into core in a later 8.x release or in D9.

Wim Leers’s picture

Issue tags: +API-First Initiative

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Grayside’s picture

I hadn't heard of application/vnd.error+json somehow, despite lots of work around HAL resources. The post example looks a little more obscure from a DX perspective, and I do not see any benefits not present in api-problem except for more explicit direction on how to handle multiple errors.

REST should use one problem format, so a format that mostly makes sense to HAL-focused eyes would be out of place in some cases.

Crell’s picture

Api-Problem is now a finalized spec, and my library (linked above) has been updated for the final RFC in the 2.0 version. There will be other features added to the library in time but for now all the core capabilities are there. (Unless we want to move it to be evolvable, a la PSR-7?)

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Title: Serve REST errors as application/api-problem+json OR application/vnd.error+json » RFC 7807: Serve REST errors as application/api-problem+json OR application/vnd.error+json
Category: Task » Feature request

https://tools.ietf.org/html/rfc7807 was ratified in March 2016.

Wim Leers’s picture

Status: Postponed » Active

Note that #2808233: REST 403 responses don't tell the user *why* access is not granted: requires deep Drupal understanding to figure out already helped improve our REST error responses a lot. And thanks to #2737719: EntityResource: Provide comprehensive test coverage: for every entity type, every format, every method, we're thoroughly testing every single one of our responses.

It's now more feasible than ever to adopt this. The main question is: do we want this?

Wim Leers’s picture

Also, I wonder if #2856110: [PP-1] Expose entity validation errors in a machine readable REST API: is basically a duplicate of this? It focuses on entity validation errors. But if we're going to do that, we should just adopt this RFC right away, rather than first creating our own format, and then causing disruption for REST clients by moving from HEAD's validation error responses (which are basically unparseable plain text) to the custom JSON format there and then finally to the format defined by RFC 7807.

Wim Leers’s picture

Title: RFC 7807: Serve REST errors as application/api-problem+json OR application/vnd.error+json » RFC 7807: "Problem Details for HTTP APIs" — serve REST error responses as application/problem+json

Updating title to match the RFC.

Wim Leers’s picture

In #14+#17, I updated the issue title to choose RFC 7807. I had missed for a moment that the Is said:

There seem to be two different approaches which are gaining traction, api-problem from the IETF and vnd.error from the HAL community.

Well, since then, a clear winner emerged: RFC 7807/application/problem+json from the IETF (ratified in March 2016) — application/vnd.error+json from the HAL community was last updated on September 9, 2014.

So, let's be more clear/explicit.

Wim Leers’s picture

… and the title already was explicit/clear, but the IS was not yet.

Rather than writing an entirely new IS, let's copy/paste the one from #2811483: Adopt Problem Details RFC for REST Error Responses, which is basically a 100% duplicate of this issue. Many thanks to @Grayside (please credit him!) for writing that.

dawehner’s picture

I'm wondering whether we can somehow shortcut some of the list entries explained in issue summary.
We have a concrete problem out there and some of those would require quite a lot of work:

Decide from a product standpoint if this is a good direction for the Drupal REST implementation
Create a facility to support message output in the application/problem+json or application/problem+xml format.
Perform an initial survey of REST error messages to determine categories.
For each error category, find or prepare a documentation page to server as the type URL.
Convert errors thrown in the course of REST responses to be handled by the new error response mechanism.

I'm wondering for example whether we could for now just use a non existing URL for the errors?

Wim Leers’s picture

I'm wondering for example whether we could for now just use a non existing URL for the errors?

+1, we do the same for link relation types: see core/core.link_relation_types.yml, specifically the top of the file, where it says:

# Drupal core's extension relation types.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dawehner’s picture

FileSize
1.37 KB

I wanted to create a document on drupal.org at least starting, but then I realized, you need some special permission for that :(
I'll continue with this document maybe tomorrow or on the weekend.

Wim Leers’s picture

👍

e0ipso’s picture

Since this is a specific format, how is this going to be negotiated? I worry about formats that have their own spec for errors being trampled by this one.

I miss in the IS in what formats this is going to be triggered. Does this only apply to our JSON format in core? Does also apply to HAL? Is this going to apply everywhere and it's the responsibility of any other format to override it? I assume it's the latter, since we already have a format for errors and that's what we're trying to replace.

We're conflating unopinionated / unknown formats with an opinionated format for errors. That may be good, but we need to aknowledge it as a feature.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

@edgab has opened a JSON API support request/documentation issue specifically about how to deal with the currently implemented validation error message structure, and how to actually use that in a client application.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

FYI: The jsonapi.module effectively already implements this, because the JSON:API spec has absorbed the relevant portions from RFC7807. See https://jsonapi.org/format/#error-objects. Even the language is lifted from RFC7807, for example:

RFC7807
     "title" (string) - A short, human-readable summary of the problem
      type.  It SHOULD NOT change from occurrence to occurrence of the
      problem, except for purposes of localization
JSON:API spec
title: a short, human-readable summary of the problem that SHOULD NOT change from occurrence to occurrence of the problem, except for purposes of localization.

That doesn't change anything here, but I just wanted to call it out explicitly that this issue applies only to rest.module.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.