ResponseSizeError.ts
This file
Path
source/errors/ResponseSizeError.ts
30 lines · 909 bytes · TypeScript
Last changed by
Add `maxResponseSize` optionSindre Sorhusbefore adoption0d59458
History of this fileTake it
Add `maxResponseSize` optionSindre Sorhusbefore adoption0d59458
lines 1–30why, one step back1import type {KyRequest} from '../types/request.js';2import {KyError} from './KyError.js';34/**5Error thrown when the response body exceeds `maxResponseSize`. It has a `request` property with the `Request` object and a `maxResponseSize` property with the configured limit in bytes.67@example8```9import ky, {isResponseSizeError} from 'ky';1011try {12 await ky('https://example.com/data', {maxResponseSize: 1024}).json();13} catch (error) {14 if (isResponseSizeError(error)) {15 console.log(`Response exceeded ${error.maxResponseSize} bytes`);16 }17}18```19*/20export class ResponseSizeError extends KyError {21 override name = 'ResponseSizeError';22 request: KyRequest;23 maxResponseSize: number;2425 constructor(request: Request, maxResponseSize: number) {26 super(`Response body exceeded ${maxResponseSize} bytes: ${request.method} ${request.url}`);27 this.request = request;28 this.maxResponseSize = maxResponseSize;29 }30}