# Errors and reasons

An HTTP/JSON error is an Error carrying an ErrorInfo. Branch on its reason, not the message.

A failed request returns an HTTP status and a JSON `Error`, in the shape of [`google.rpc.Status`](https://cloud.google.com/apis/design/errors#error_model).
Its `details` carry an `ErrorInfo` (a [`google.rpc.ErrorInfo`](https://google.aip.dev/193)) whose `reason` says why the request failed:

```json
{
  "code": 5,
  "message": "not_found",
  "details": [
    {
      "@type": "type.googleapis.com/google.rpc.ErrorInfo",
      "reason": "PRODUCT_NOT_FOUND",
      "domain": "drophawk.net"
    }
  ]
}
```

- `code` is the gRPC status code that matches the HTTP status (`5` is `NOT_FOUND`).
- `message` is the code's name, such as `not_found`: stable, for logs, and not for display.
- `details` holds an `ErrorInfo`, whose `@type` is `type.googleapis.com/google.rpc.ErrorInfo` and whose `domain` is `drophawk.net`.
  Its `reason` is what to branch on: one HTTP status can be shared by several causes.
  Some reasons add context in `metadata`, as string values.

`details` is a list of typed entries: an `ErrorInfo`, or an `ErrorDetail` of a type you do not know yet, and more may be added within v1.
Find the entry whose `@type` is `type.googleapis.com/google.rpc.ErrorInfo`, and skip any entry whose `@type` you do not recognise.

Every HTTP/JSON error carries that `ErrorInfo`.
If a response ever arrives without one, or with a reason you do not recognise, decide what to do from the HTTP status alone.

## Reasons

| HTTP | Reason | Meaning |
| --- | --- | --- |
| 404 | `PRODUCT_NOT_FOUND` | No Product is known by the identifier, or it has been removed from the catalog. |
| 400 | `INVALID_IDENTIFIER` | The identifier cannot name any Product. |
| 400 | `INVALID_QUERY` | The search query q is blank, over 256 bytes, or has control characters. |
| 400 | `INVALID_PAGE_TOKEN` | The page_token is malformed, was issued for a different q, or was issued before a change to search ordering. |
| 400 | `INVALID_PAGE_SIZE` | The page_size is negative. |
| 400 | `EMPTY_BATCH` | A batch request names no identifiers. |
| 400 | `BATCH_TOO_LARGE` | A batch request names more than 50 identifiers; metadata "max_identifiers" is the limit. |
| 400 | `INVALID_REQUEST` | The request is malformed. |
| 404 | `NOT_FOUND` | There is no such endpoint. |
| 405 | `METHOD_NOT_ALLOWED` | The endpoint does not accept that HTTP method. |
| 413 | `REQUEST_TOO_LARGE` | The request body is over 64 KiB. Retrying the same request cannot succeed; send less. |
| 429 | `RATE_LIMITED` | Too many requests; metadata "retry_after_seconds" repeats Retry-After. |
| 500 | `INTERNAL` | DropHawk failed; retry later. |
| 503 | `UNAVAILABLE` | DropHawk is briefly unable to serve; retry later. |

New reasons may be added within v1, as the [versioning and deprecation policy](https://developers.drophawk.net/guides/versioning-and-deprecation.md) allows.

## Retrying

- `PRODUCT_NOT_FOUND`, `INVALID_IDENTIFIER`, `INVALID_REQUEST`, `NOT_FOUND` and `METHOD_NOT_ALLOWED` will fail the same way again.
  Fix the request instead of retrying it.
- `RATE_LIMITED` succeeds once you wait: the `Retry-After` header, and the `retry_after_seconds` metadata, say how many seconds.
  See [Rate limits](https://developers.drophawk.net/guides/rate-limits.md).
- `INTERNAL` and `UNAVAILABLE`, and any other `5xx`, mean something failed on DropHawk's side or it is briefly unable to serve.
  Retry with exponential backoff.

Error responses are sent with `Cache-Control: no-store`, so no cache keeps a 404 after the Product appears; see [CORS and caching](https://developers.drophawk.net/guides/cors-and-caching.md).

## Connect and gRPC

A Connect or gRPC client gets its protocol's own error model instead of the JSON above, carrying the same `ErrorInfo` whenever the API raised the error.
A request counts as Connect or gRPC only when it carries a protocol marker: a `Connect-Protocol-Version` header, `connect=v1` in a Connect `GET`'s query, or a gRPC, gRPC-Web or Connect streaming `Content-Type`.
Without one, even a `GET` to an RPC path such as `/drophawk.developer.v1.ProductService/GetProduct` is HTTP/JSON, and gets the JSON error above (`404` with `NOT_FOUND`).
The message is the error code's name in every protocol, so branch on the reason there too.

Errors the RPC framework raises before the API sees the request carry no `ErrorInfo`:

- a request body that does not decode: `invalid_argument`, with the message `invalid request body`;
- a request body over 64 KiB: `resource_exhausted` - not rate limiting, which always carries `RATE_LIMITED`;
- a method that does not exist.

Handle a missing `ErrorInfo` by the error code.

## Not errors

A `301 Moved Permanently` means the Product was merged into another.
Follow `Location`, and update the ModelSlug you stored; see [Merged Products](https://developers.drophawk.net/guides/identifiers.md#merged-products).

## Reserved statuses

`401` and `403` are reserved for a future keyed tier and are not returned in v1; see [Authentication](https://developers.drophawk.net/guides/authentication.md).
