From 70011e9155109aa6c4412399ee3ce8a70af5386a Mon Sep 17 00:00:00 2001 From: Ian Bull Date: Wed, 21 Aug 2024 01:42:02 -0400 Subject: [PATCH] refactor(archive,cache,datetime,fmt,front-matter): align error messages to the style guide (#5706) Co-authored-by: Yoshiya Hinosawa --- archive/tar.ts | 18 ++++++++------ archive/untar.ts | 6 +++-- cache/_serialize_arg_list.ts | 2 +- datetime/_date_time_formatter.ts | 12 ++++++---- datetime/_date_time_formatter_test.ts | 2 +- fmt/printf.ts | 34 ++++++++++++++++++++------- fmt/printf_test.ts | 8 +++++-- front_matter/_shared.ts | 2 +- 8 files changed, 56 insertions(+), 28 deletions(-) diff --git a/archive/tar.ts b/archive/tar.ts index 49bbb282e..32cc21aae 100644 --- a/archive/tar.ts +++ b/archive/tar.ts @@ -307,7 +307,7 @@ export class Tar { */ async append(filenameInArchive: string, source: TarOptions) { if (typeof filenameInArchive !== "string") { - throw new Error("file name not specified"); + throw new Error("Cannot append data: File name is not a string"); } let fileName = filenameInArchive; @@ -332,7 +332,7 @@ export class Tar { i--; } const errMsg = - "ustar format does not allow a long file name (length of [file name" + + "Cannot append data: The 'ustar' format does not allow a long file name (length of [file name" + "prefix] + / + [file name] must be shorter than 256 bytes)"; if (i < 0 || fileName.length > 100) { throw new Error(errMsg); @@ -368,18 +368,18 @@ export class Tar { if (typeof source.owner === "string" && source.owner.length >= 32) { throw new Error( - "ustar format does not allow owner name length >= 32 bytes", + "Cannot append data: The 'ustar' format does not allow owner name length >= 32 bytes", ); } if (typeof source.group === "string" && source.group.length >= 32) { throw new Error( - "ustar format does not allow group name length >= 32 bytes", + "Cannot append data: The 'ustar' format does not allow group name length >= 32 bytes", ); } const fileSize = info?.size ?? source.contentSize; if (fileSize === undefined) { - throw new TypeError("fileSize must be set"); + throw new TypeError("Cannot append data: The file size is not defined"); } const type = source.type @@ -460,7 +460,9 @@ export class Tar { readers.push(new Buffer(headerArr)); if (!reader) { if (filePath === undefined) { - throw new TypeError("filePath must be defined"); + throw new TypeError( + "Cannot get the reader for the tar archive: FilePath is not defined", + ); } reader = new FileReader(filePath); } @@ -468,7 +470,9 @@ export class Tar { // to the nearest multiple of recordSize if (tarData.fileSize === undefined) { - throw new TypeError("fileSize must be set"); + throw new TypeError( + "Cannot get the reader for the tar archive: FileSize is not defined", + ); } readers.push( new Buffer( diff --git a/archive/untar.ts b/archive/untar.ts index 8953c0830..d513acd23 100644 --- a/archive/untar.ts +++ b/archive/untar.ts @@ -424,13 +424,15 @@ export class Untar { // EOF return null; } - throw new Error("checksum error"); + throw new Error("Cannot validate checksum"); } const magic = decoder.decode(header.ustar); if (magic.indexOf("ustar")) { - throw new Error(`unsupported archive format: ${magic}`); + throw new Error( + `Cannot validate the header as it has unsupported archive format: ${magic}`, + ); } return header; diff --git a/cache/_serialize_arg_list.ts b/cache/_serialize_arg_list.ts index 43e0395bf..0efd040a3 100644 --- a/cache/_serialize_arg_list.ts +++ b/cache/_serialize_arg_list.ts @@ -54,7 +54,7 @@ export function _serializeArgList( } // Non-weak keys other than `Symbol.for(...)` are handled by the branches above. throw new Error( - "Should be unreachable. Please open an issue at https://github.com/denoland/std/issues/new", + "Should be unreachable: please open an issue at https://github.com/denoland/std/issues/new", ); } diff --git a/datetime/_date_time_formatter.ts b/datetime/_date_time_formatter.ts index c6a09f952..b3f6c3c50 100644 --- a/datetime/_date_time_formatter.ts +++ b/datetime/_date_time_formatter.ts @@ -397,7 +397,7 @@ export class DateTimeFormatter { value = /^\d{1,2}/.exec(string)?.[0] as string; if (part.hour12 && parseInt(value) > 12) { console.error( - `Trying to parse hour greater than 12. Use 'H' instead of 'h'.`, + `Trying to parse hour greater than 12, use 'H' instead of 'h'.`, ); } break; @@ -406,7 +406,7 @@ export class DateTimeFormatter { value = /^\d{2}/.exec(string)?.[0] as string; if (part.hour12 && parseInt(value) > 12) { console.error( - `Trying to parse hour greater than 12. Use 'HH' instead of 'hh'.`, + `Trying to parse hour greater than 12, use 'HH' instead of 'hh'.`, ); } break; @@ -475,7 +475,7 @@ export class DateTimeFormatter { value = "PM"; break; default: - throw new Error(`dayPeriod '${value}' is not supported.`); + throw new Error(`DayPeriod '${value}' is not supported.`); } break; } @@ -490,12 +490,14 @@ export class DateTimeFormatter { } default: - throw Error(`${part.type} ${part.value}`); + throw Error( + `Cannot format the date, the value (${part.value}) of the type (${part.type}) is given`, + ); } if (!value) { throw Error( - `value not valid for part { ${type} ${value} } ${ + `Cannot format value: The value is not valid for part { ${type} ${value} } ${ string.slice( 0, 25, diff --git a/datetime/_date_time_formatter_test.ts b/datetime/_date_time_formatter_test.ts index 11a313e54..2a6409353 100644 --- a/datetime/_date_time_formatter_test.ts +++ b/datetime/_date_time_formatter_test.ts @@ -61,7 +61,7 @@ Deno.test("dateTimeFormatter.formatToParts() throws on an empty string", () => { assertThrows( () => formatter.formatToParts(""), Error, - "value not valid for part", + "Cannot format value: The value is not valid for part { year undefined } ", ); }); diff --git a/fmt/printf.ts b/fmt/printf.ts index 7a9b3d6e0..f30feb3c7 100644 --- a/fmt/printf.ts +++ b/fmt/printf.ts @@ -242,7 +242,9 @@ class Printf { } break; default: - throw Error("Should be unreachable, certainly a bug in the lib."); + throw Error( + `State ${this.state} should be unreachable, please file a bug report against Deno at https://github.com/denoland/std/issues`, + ); } } // check for unhandled args @@ -324,7 +326,9 @@ class Printf { return; // always end in verb } default: - throw new Error(`Should not be here ${this.state}, library bug!`); + throw new Error( + `State ${this.state} should be unreachable, please file a bug report against Deno at https://github.com/denoland/std/issues`, + ); } // switch state } } @@ -408,7 +412,9 @@ class Printf { break; } default: - throw new Error("can't be here. bug."); + throw new Error( + `State ${this.state} should be unreachable, please file a bug report against Deno at https://github.com/denoland/std/issues`, + ); } // switch state } } @@ -417,7 +423,9 @@ class Printf { handlePositional() { if (this.format[this.i] !== "[") { // sanity only - throw new Error("Can't happen? Bug."); + throw new Error( + "Should be unreachable, please file a bug report against Deno at https://github.com/denoland/std/issues", + ); } let positional = 0; const format = this.format; @@ -450,7 +458,9 @@ class Printf { // deno-lint-ignore no-explicit-any const arg = this.args[this.argNum] as any; if ((arg || {}).constructor.name !== "Array") { - throw new Error(`arg ${arg} is not an array. Todo better error handling`); + throw new Error( + `Cannot handle less than '<' flag: 'arg' is not an array`, + ); } let str = "[ "; for (let i = 0; i !== arg.length; ++i) { @@ -611,7 +621,9 @@ class Printf { prefix += "0x"; break; default: - throw new Error("cannot handle base: " + radix); + throw new Error( + `Cannot handle the radix ${radix}: only 2, 8, 16 are supported`, + ); } } // don't add prefix in front of value truncated by precision=0, val=0 @@ -699,7 +711,9 @@ class Printf { const m = n.toExponential().match(FLOAT_REGEXP); if (!m) { - throw Error("can't happen, bug"); + throw new Error( + "Should be unreachable, please file a bug report against Deno at https://github.com/denoland/std/issues", + ); } const precision = this.flags.precision !== -1 ? this.flags.precision @@ -817,7 +831,9 @@ class Printf { const m = n.toExponential().match(FLOAT_REGEXP); if (!m) { - throw Error("can't happen"); + throw new Error( + "Should be unreachable, please file a bug report against Deno at https://github.com/denoland/std/issues", + ); } const X = parseInt(m[F.exponent]!) * (m[F.esign] === "-" ? -1 : 1); @@ -881,7 +897,7 @@ class Printf { } default: throw new Error( - "currently only number and string are implemented for hex", + `Cannot format hex, only number and string are supported for hex formatting: ${typeof val} is given`, ); } } diff --git a/fmt/printf_test.ts b/fmt/printf_test.ts index b31e563b2..ba95ff3ac 100644 --- a/fmt/printf_test.ts +++ b/fmt/printf_test.ts @@ -177,7 +177,7 @@ Deno.test("sprintf() handles hex", function () { assertThrows( () => sprintf("%x", {}), Error, - "currently only number and string are implemented for hex", + "Cannot format hex, only number and string are supported for hex formatting: object is given", ); }); Deno.test("sprintf() handles heX", function () { @@ -729,7 +729,11 @@ Deno.test("sprintf() handles errors", function () { }); Deno.test("sprintf() throws with d with sharp option", () => { - assertThrows(() => sprintf("%#d", 1.1), Error, "cannot handle base: 10"); + assertThrows( + () => sprintf("%#d", 1.1), + Error, + "Cannot handle the radix 10: only 2, 8, 16 are supported", + ); }); Deno.test("printf() prints the result synchronously", () => { diff --git a/front_matter/_shared.ts b/front_matter/_shared.ts index 873ff91c8..9914afa64 100644 --- a/front_matter/_shared.ts +++ b/front_matter/_shared.ts @@ -39,5 +39,5 @@ export function recognize( if (RECOGNIZE_REGEXP_MAP.get(format)?.test(firstLine)) return format; } - throw new TypeError(`Unsupported front matter format.`); + throw new TypeError("Unsupported front matter format"); }