Source code:
All Got errors contain various metadata, such as:
code- A string likeERR_NON_2XX_3XX_RESPONSE,options- An instance ofOptions,request- An instance of Got Stream,response(optional) - An instance of Got Response,timings(optional) - Points toresponse.timings.
Note
SchemaValidationError is the exception: it comes from the user-provided schema instead of the request lifecycle, so the only metadata it adds is the schema issues.
Read the article here.
Note
- The error codes may differ when the root error has a
codeproperty set. - The root error will be propagated as is via the
causeproperty.
Code: ERR_GOT_REQUEST_ERROR
When a request fails. Contains a code property with error class code, like ECONNREFUSED. All the errors below inherit this one, except SchemaValidationError.
Code: ERR_CACHE_ACCESS
When a cache method fails, for example, if the database goes down or there's a filesystem error.
Code: ERR_READING_RESPONSE_STREAM or ERR_HTTP_CONTENT_LENGTH_MISMATCH
When reading from response stream fails.
The error code will be ERR_HTTP_CONTENT_LENGTH_MISMATCH when the strictContentLength option is enabled and the server specifies a content-length header but the actual number of bytes received doesn't match.
Code: ERR_BODY_PARSE_FAILURE
When server response code is 2xx, and parsing body fails. Includes a response property.
When the response body does not match the Standard Schema validator passed to promise.json(schema). Includes an issues property with the issues from the schema. Each issue has a message and an optional path.
It intentionally does not extend RequestError, because the request succeeded. The user-provided schema did not accept the data, so it has no code, options, request, response or timings property, and it is not passed to beforeError hooks.
import got, {SchemaValidationError} from 'got';
import {z} from 'zod';
const userSchema = z.object({name: z.string()});
try {
const user = await got('https://example.com/user').json(userSchema);
console.log(user.name);
} catch (error) {
if (error instanceof SchemaValidationError) {
console.error(error.issues);
} else {
throw error;
}
}Code: ERR_UPLOAD
When the request body is a stream and an error occurs while reading from that stream.
Code: ERR_NON_2XX_3XX_RESPONSE
When the request is unsuccessful.
A request is successful when the status code of the final request is 2xx or 3xx.
When following redirects, a request is successful only when the status code of the final request is 2xx.
Note
304 responses are always considered successful.
Code: ERR_TOO_MANY_REDIRECTS
When the server redirects you more than ten times. Includes a response property.
Note
This error is not public.
Code: ERR_UNSUPPORTED_PROTOCOL
When given an unsupported protocol.
Code: ETIMEDOUT
When the request is aborted due to a timeout. Includes an event (a string) property along with timings.
Code: ERR_RETRYING
Always triggers a new retry when thrown.
Code: ERR_ABORTED
When the request is aborted with AbortController.abort().