Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ParseError class to extend SyntaxError #255

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading