The error shape
Every error has the same shape. code is stable and meant for your code. message is a sentence for people and may change. fields appears only for validation errors.
{
"error": {
"code": "validation",
"message": "Check the highlighted fields and try again.",
"fields": [
{ "path": "firstName", "message": "Required" },
{ "path": "email", "message": "Invalid email" }
]
}
}A path in fields names the part of the body that was wrong. For an id in the address that is not 24 characters of hexadecimal, the path is empty and the message is Invalid id.
Error codes
| HTTP status | Code | When it happens |
|---|---|---|
| 400 | validation | A field is missing or not valid, a query parameter is out of range, or an id is malformed. Read fields for what to fix. |
| 400 | passwordRequired | You handed a new organization or clinic to a person who does not have an account yet, without giving a password. |
| 400 | pickClinic | You tried to create a patient, provider or order while working in an organization. Choose a clinic first with x-clinic-id. |
| 400 | clinicRequired | A super admin created a patient, provider or order without choosing a clinic. |
| 400 | parentMustBeOrganization | You tried to create something under a clinic. Only an organization can contain sub-organizations or clinics. |
| 400 | offerNotFound | An order line names a SKU that is not in the catalog. |
| 401 | unauthenticated | There is no session, or it is invalid or expired. Sign in. |
| 401 | invalidCredentials | The email or password is not right. |
| 403 | forbidden | Your role is not allowed to do this, or you may not create organizations. |
| 403 | accountDisabled | The account has been disabled. |
| 404 | notFound | There is nothing at this address, or the item exists outside your scope or environment. |
| 404 | patientNotFound | The patient of an order is not in the clinic you are working in. |
| 409 | emailTaken | An account with this email already exists. |
| 409 | alreadyCancelled | The order was already cancelled. |
| 500 | server | Something went wrong on the HomelyRx side. Try again. |
Handling errors
- Switch on
error.code, not on the message text. - On
401, sign in again and repeat the call once. - On
400withvalidation, show or logfields. Do not retry the same body. - On
404, check the id, the clinic you are working in (x-clinic-id) and the environment (x-hrx-env). - On
500, retry after a short wait, and contact HomelyRx if it keeps happening.
const res = await fetch(`${API}/v1/orders/122703/cancel`, { method: 'POST', credentials: 'include' });
if (!res.ok) {
const { error } = await res.json();
if (error.code === 'alreadyCancelled') console.log('Nothing to do');
else throw new Error(`${error.code}: ${error.message}`);
}r = s.post(f"{api}/v1/orders/122703/cancel")
if not r.ok:
error = r.json()["error"]
if error["code"] != "alreadyCancelled":
raise RuntimeError(f'{error["code"]}: {error["message"]}')