# Are error objects optional in error responses?

**URL:** <https://discuss.jsonapi.org/t/are-error-objects-optional-in-error-responses/1083>\
**Category:** Uncategorized\
**Created:** [August 23, 2017, 6:31pm UTC](https://discuss.jsonapi.org/t/are-error-objects-optional-in-error-responses/1083 "2017-08-23T18:31:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Garrick](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/garrick/32/481_2.png) [@Garrick](https://discuss.jsonapi.org/u/Garrick)\
**Post date:** [August 23, 2017, 6:31pm UTC](https://discuss.jsonapi.org/t/are-error-objects-optional-in-error-responses/1083/1 "2017-08-23T18:31:05Z")

</div>

With error responses, such as 4xx and 5xx, are error objects in the response optional?

From reading [http://jsonapi.org/format/#errors](http://jsonapi.org/format/#errors) it seems like they are, but I’d like to confirm.

Thanks!

---

<div class="post-metadata">

**Author:** ![steveklabnik](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/steveklabnik/32/2_2.png) [@steveklabnik](https://discuss.jsonapi.org/u/steveklabnik)\
**Post date:** [August 23, 2017, 7:52pm UTC](https://discuss.jsonapi.org/t/are-error-objects-optional-in-error-responses/1083/2 "2017-08-23T19:52:58Z")

</div>

So, [JSON:API — Latest Specification (v1.1)](http://jsonapi.org/format/#document-top-level)

> A document MUST contain at least one of the following top-level members:
> 
> - data: the document’s “primary data”
> - errors: an array of error objects
> - meta: a meta object that contains non-standard meta-information.
> 
> The members data and errors MUST NOT coexist in the same document.

You are correct that the spec does not say that you `MUST` have at least one error document inside of the top-level errors key.

---

<div class="post-metadata">

**Author:** ![beauby](https://avatars.discourse-cdn.com/v4/letter/b/f475e1/32.png) [@beauby](https://discuss.jsonapi.org/u/beauby)\
**Post date:** [September 15, 2017, 1:47pm UTC](https://discuss.jsonapi.org/t/are-error-objects-optional-in-error-responses/1083/3 "2017-09-15T13:47:00Z")

</div>

Also, the spec does not state that you `MUST` respond with any document, so it is perfectly valid to respond with an HTTP error code and no body.

---

<div class="post-metadata">

**Author:** ![ybringler](https://avatars.discourse-cdn.com/v4/letter/y/7993a0/32.png) [@ybringler](https://discuss.jsonapi.org/u/ybringler)\
**Post date:** [August 28, 2018, 1:27pm UTC](https://discuss.jsonapi.org/t/are-error-objects-optional-in-error-responses/1083/4 "2018-08-28T13:27:11Z")

</div>

Also at [JSON:API — Latest Specification (v1.1)](http://jsonapi.org/format/#document-top-level:)

> A JSON object **MUST** be at the root of every JSON API request and response **containing data**.

(Emphasis added.)

So (a possible reading is that) if there’s no data being returned, for example because there is an error, then there is no requirement to return a JSON object.

However, it also says (ibid)

> This object defines a document’s “top level”.
> 
> A document **MUST** contain at least one of the following top-level members

‘This object defines a document’s “top level”.’ seems to imply that the concept of a document exists independently of any JSON. If a document can be said to _have_ a top level, that means that there are two distinct concepts: a document, and it’s top level, and that a document with no top level is still called a document.

Thus, when the spec says

> A **document**** MUST** contain at least one of the following top-level members

(emphasis added) this seems to be saying that _all responses_ must contain a JSON object, as specified ibid in the subsequent lines.

In order for the document to avoid the implication of JSON necessity, it would (seemingly) have to be written

> A **document’s top level** must contain…

Which would leave room to say that a document doesn’t need a top level.

To conclude, I don’t know if it’s %100 clear what “document” and “top level” mean, but it seems that JSON must be returned always.

Which leaves us with the first quote

> … **containing data**.

It could be explained that an error is data, especially because (I hope) there’s some sort of description included.

This would perhaps exclude if (due to routing, validation, run time errors, etc ) the request never reaches an actual JSON API end point, and perhaps means to say that there isn’t a requirement to ensure that all server messages be accompanied with JSON.

---

<div class="post-metadata">

**Author:** ![r-maya](https://avatars.discourse-cdn.com/v4/letter/r/9de0a6/32.png) [@r-maya](https://discuss.jsonapi.org/u/r-maya)\
**Post date:** [September 12, 2019, 8:30am UTC](https://discuss.jsonapi.org/t/are-error-objects-optional-in-error-responses/1083/5 "2019-09-12T08:30:01Z")

</div>

Can there be errors in a Request? In the specification, it only mentions that one of the three top level should be present in the document. So does that mean errors can be present in the request too?

---

<div class="post-metadata">

**Author:** ![maark](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/maark/32/184_2.png) [@maark](https://discuss.jsonapi.org/u/maark)\
**Post date:** [September 12, 2019, 11:00pm UTC](https://discuss.jsonapi.org/t/are-error-objects-optional-in-error-responses/1083/6 "2019-09-12T23:00:03Z")

</div>

> [@r-maya](#):
>
> Can there be errors in a Request? In the specification, it only mentions that one of the three top level should be present in the document. So does that mean errors can be present in the request too?

This is another question that deserves its own topic.
