refactor(archive,cache,datetime,fmt,front-matter): align error messages to the style guide (#5706)

Co-authored-by: Yoshiya Hinosawa <stibium121@gmail.com>
This commit is contained in:
Ian Bull 2024-08-21 01:42:02 -04:00 committed by GitHub
parent 8e96c1992f
commit 70011e9155
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 56 additions and 28 deletions

View File

@ -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(

View File

@ -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;

View File

@ -54,7 +54,7 @@ export function _serializeArgList<Return>(
}
// 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",
);
}

View File

@ -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,

View File

@ -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 } ",
);
});

View File

@ -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`,
);
}
}

View File

@ -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", () => {

View File

@ -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");
}