Skip to content

Commit

Permalink
Refactor ParseError class to extend SyntaxError (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiazya authored Jun 11, 2024
1 parent 88fbef6 commit 49af679
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/plenty-guests-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"web-csv-toolbox": patch
---

Refactor ParseError class to extend SyntaxError
89 changes: 89 additions & 0 deletions src/common/errors.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { describe, expect, it } from "vitest";
import { InvalidOptionError, ParseError } from "./errors";

describe("InvalidOptionError", () => {
it("should be an instance of Error", () => {
// The InvalidOptionError class should be an instance of Error.
expect(new InvalidOptionError()).toBeInstanceOf(Error);
});

it("should have a name property", () => {
// The InvalidOptionError class should have
// a name property equal to "InvalidOptionError".
expect(new InvalidOptionError().name).toStrictEqual("InvalidOptionError");
});

it("should have a message property", () => {
// The InvalidOptionError class should have
// a message property equal to empty string.
expect(new InvalidOptionError().message).toStrictEqual("");

// The InvalidOptionError class should have
// a message property equal to the provided value.
expect(new InvalidOptionError("message").message).toStrictEqual("message");
});

it("should have a cause property", () => {
// The InvalidOptionError class should have
// a cause property equal to undefined.
expect(new InvalidOptionError().cause).toBeUndefined();

// The InvalidOptionError class should have
// a cause property equal to the provided value.
expect(
new InvalidOptionError("", { cause: new Error() }).cause,
).toStrictEqual(new Error());
});
});

describe("ParseError", () => {
it("should be an instance of Error", () => {
// The ParseError class should be an instance of Error.
expect(new ParseError()).toBeInstanceOf(Error);
});

it("should be an instance of SyntaxError", () => {
// The ParseError class should be an instance of SyntaxError.
expect(new ParseError()).toBeInstanceOf(SyntaxError);
});

it("should have a name property", () => {
// The ParseError class should have
// a name property equal to "ParseError".
expect(new ParseError().name).toStrictEqual("ParseError");
});

it("should have a message property", () => {
// The ParseError class should have
// a message property equal to empty string.
expect(new ParseError().message).toStrictEqual("");

// The ParseError class should have a message property.
expect(new ParseError("message").message).toStrictEqual("message");
});

it("should have a position property", () => {
// The ParseError class should have
// a position property equal to undefined.
expect(new ParseError().position).toStrictEqual(undefined);

// The ParseError class should have
// a position property equal to the provided value.
expect(
new ParseError("", { position: { line: 1, column: 1, offset: 0 } })
.position,
).toStrictEqual({ line: 1, column: 1, offset: 0 });
});

it("should have a cause property", () => {
// The ParseError class should have
// a cause property equal to undefined.
expect(new ParseError().cause).toBeUndefined();

// The ParseError class should have
// a cause property equal to the provided value.
expect(new ParseError("", { cause: new Error() }).cause).toStrictEqual(
new Error(),
);
});
});
9 changes: 8 additions & 1 deletion src/common/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ export interface ParseErrorOptions extends ErrorOptions {

/**
* Error class for parse errors.
*
* @remarks
* This error is thrown when a parsing error occurs.
* {@link ParseError} is a subclass of {@link !SyntaxError}.
*
* This is in reference to the specification
* that the error thrown when a parse error occurs in the {@link !JSON.parse} function is {@link !SyntaxError}.
*/
export class ParseError extends Error {
export class ParseError extends SyntaxError {
/**
* The position where the error occurred.
*/
Expand Down

0 comments on commit 49af679

Please sign in to comment.