Add TooLarge error code for buffers (#1298)

In collaboration with @yushimatenjin
This commit is contained in:
binaryta 2018-12-10 05:38:30 +09:00 committed by Ryan Dahl
parent f2447f6a23
commit c427c2df42
3 changed files with 26 additions and 2 deletions

View file

@ -6,6 +6,7 @@
import { Reader, Writer, ReadResult } from "./io";
import { assert } from "./util";
import { TextDecoder } from "./text_encoding";
import { DenoError, ErrorKind } from "./errors";
// MIN_READ is the minimum ArrayBuffer size passed to a read call by
// buffer.ReadFrom. As long as the Buffer has at least MIN_READ bytes beyond
@ -170,7 +171,10 @@ export class Buffer implements Reader, Writer {
// don't spend all our time copying.
copyBytes(this.buf, this.buf.subarray(this.off));
} else if (c > MAX_SIZE - c - n) {
throw Error("ErrTooLarge"); // TODO DenoError(TooLarge)
throw new DenoError(
ErrorKind.TooLarge,
"The buffer cannot be grown beyond the maximum size."
);
} else {
// Not enough space anywhere, we need to allocate.
const buf = new Uint8Array(2 * c + n);