Skip to content

Commit

Permalink
Clarify error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken committed Oct 30, 2021
1 parent c43754d commit d8abd8d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
28 changes: 14 additions & 14 deletions src/Float16Array.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import {
isTypedArray,
} from "./_util/is.mjs";
import {
ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER,
CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT,
CANNOT_MIX_BIGINT_AND_OTHER_TYPES,
CONSTRUCTOR_IS_NOT_A_OBJECT,
DERIVED_TYPEDARRAY_CONSTRUCTOR_CREATED_AN_ARRAY_WHICH_WAS_TOO_SMALL,
DERIVED_CONSTRUCTOR_CREATED_TYPEDARRAY_OBJECT_WHICH_WAS_TOO_SMALL_LENGTH,
OFFSET_IS_OUT_OF_BOUNDS,
REDUCE_OF_EMPTY_ARRAY_WITH_NO_INITIAL_VALUE,
SPECIESCONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY,
THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED,
SPECIES_CONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY_OBJECT,
THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT,
THIS_CONSTRUCTOR_IS_NOT_A_SUBCLASS_OF_FLOAT16ARRAY,
THIS_IS_NOT_A_FLOAT16ARRAY,
THIS_IS_NOT_A_FLOAT16ARRAY_OBJECT,
} from "./_util/messages.mjs";
import {
ArrayBufferIsView,
Expand Down Expand Up @@ -111,7 +111,7 @@ function hasFloat16ArrayBrand(target) {
return false;
}
if (!isObject(constructor)) {
throw NativeTypeError(CONSTRUCTOR_IS_NOT_A_OBJECT);
throw NativeTypeError(THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT);
}

return ReflectHas(constructor, brand);
Expand All @@ -133,7 +133,7 @@ export function isFloat16Array(target) {
*/
function assertFloat16Array(target) {
if (!isFloat16Array(target)) {
throw NativeTypeError(THIS_IS_NOT_A_FLOAT16ARRAY);
throw NativeTypeError(THIS_IS_NOT_A_FLOAT16ARRAY_OBJECT);
}
}

Expand All @@ -148,7 +148,7 @@ function assertSpeciesTypedArray(target, count) {
const isTargetTypedArray = isTypedArray(target);

if (!isTargetFloat16Array && !isTargetTypedArray) {
throw NativeTypeError(SPECIESCONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY);
throw NativeTypeError(SPECIES_CONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY_OBJECT);
}

if (typeof count === "number") {
Expand All @@ -162,7 +162,7 @@ function assertSpeciesTypedArray(target, count) {

if (length < count) {
throw NativeTypeError(
DERIVED_TYPEDARRAY_CONSTRUCTOR_CREATED_AN_ARRAY_WHICH_WAS_TOO_SMALL
DERIVED_CONSTRUCTOR_CREATED_TYPEDARRAY_OBJECT_WHICH_WAS_TOO_SMALL_LENGTH
);
}
}
Expand All @@ -182,15 +182,15 @@ function getFloat16BitsArray(float16) {
if (float16bitsArray !== undefined) {
const buffer = TypedArrayPrototypeGetBuffer(float16bitsArray);
if (IsDetachedBuffer(buffer)) {
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED);
throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER);
}
return float16bitsArray;
}

// @ts-ignore
const buffer = float16.buffer;
if (IsDetachedBuffer(buffer)) {
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED);
throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER);
}

// from another Float16Array instance (a different version?)
Expand Down Expand Up @@ -306,7 +306,7 @@ export class Float16Array {
: NativeArrayBuffer;

if (IsDetachedBuffer(buffer)) {
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED);
throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER);
}

if (isBigIntTypedArray(input)) {
Expand Down Expand Up @@ -819,7 +819,7 @@ export class Float16Array {
if (isTypedArray(input)) {
const buffer = TypedArrayPrototypeGetBuffer(input);
if (IsDetachedBuffer(buffer)) {
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED);
throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER);
}
}

Expand Down Expand Up @@ -937,7 +937,7 @@ export class Float16Array {

const buffer = TypedArrayPrototypeGetBuffer(float16bitsArray);
if (IsDetachedBuffer(buffer)) {
throw NativeTypeError(THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED);
throw NativeTypeError(ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER);
}

let n = 0;
Expand Down
18 changes: 10 additions & 8 deletions src/_util/messages.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
export const CONSTRUCTOR_IS_NOT_A_OBJECT = "Constructor is not a object";
export const THIS_IS_NOT_A_OBJECT = "This is not a object";
export const THIS_IS_NOT_A_FLOAT16ARRAY = "This is not a Float16Array";
export const THIS_IS_NOT_AN_OBJECT = "This is not an object";
export const THIS_IS_NOT_A_FLOAT16ARRAY_OBJECT = "This is not a Float16Array object";
export const THIS_CONSTRUCTOR_IS_NOT_A_SUBCLASS_OF_FLOAT16ARRAY =
"This constructor is not a subclass of Float16Array";
export const SPECIESCONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY =
"SpeciesConstructor didn't return TypedArray";
export const DERIVED_TYPEDARRAY_CONSTRUCTOR_CREATED_AN_ARRAY_WHICH_WAS_TOO_SMALL =
"Derived TypedArray constructor created an array which was too small";
export const THIS_BUFFER_HAS_ALREADY_BEEN_DETACHED = "This buffer has already been detached";
export const THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT =
"The constructor property value is not an object";
export const SPECIES_CONSTRUCTOR_DIDNT_RETURN_TYPEDARRAY_OBJECT =
"Species constructor didn't return TypedArray object";
export const DERIVED_CONSTRUCTOR_CREATED_TYPEDARRAY_OBJECT_WHICH_WAS_TOO_SMALL_LENGTH =
"Derived constructor created TypedArray object which was too small length";
export const ATTEMPTING_TO_ACCESS_DETACHED_ARRAYBUFFER =
"Attempting to access detached ArrayBuffer";
export const CANNOT_CONVERT_UNDEFINED_OR_NULL_TO_OBJECT =
"Cannot convert undefined or null to object";
export const CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER =
Expand Down
10 changes: 5 additions & 5 deletions src/_util/spec.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { isObject } from "./is.mjs";
import {
CANNOT_CONVERT_A_BIGINT_VALUE_TO_A_NUMBER,
CONSTRUCTOR_IS_NOT_A_OBJECT,
THIS_IS_NOT_A_OBJECT,
THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT,
THIS_IS_NOT_AN_OBJECT,
} from "./messages.mjs";
import {
ArrayBufferPrototypeSlice,
Expand Down Expand Up @@ -58,7 +58,7 @@ function ToLength(target) {
*/
export function LengthOfArrayLike(arrayLike) {
if (!isObject(arrayLike)) {
throw NativeTypeError(THIS_IS_NOT_A_OBJECT);
throw NativeTypeError(THIS_IS_NOT_AN_OBJECT);
}

return ToLength(/** @type {any} */ (arrayLike).length);
Expand All @@ -72,15 +72,15 @@ export function LengthOfArrayLike(arrayLike) {
*/
export function SpeciesConstructor(target, defaultConstructor) {
if (!isObject(target)) {
throw NativeTypeError(THIS_IS_NOT_A_OBJECT);
throw NativeTypeError(THIS_IS_NOT_AN_OBJECT);
}

const constructor = target.constructor;
if (constructor === undefined) {
return defaultConstructor;
}
if (!isObject(constructor)) {
throw NativeTypeError(CONSTRUCTOR_IS_NOT_A_OBJECT);
throw NativeTypeError(THE_CONSTRUCTOR_PROPERTY_VALUE_IS_NOT_AN_OBJECT);
}

const species = constructor[SymbolSpecies];
Expand Down

0 comments on commit d8abd8d

Please sign in to comment.