Skip to content

Commit

Permalink
Remove BaseError.
Browse files Browse the repository at this point in the history
  • Loading branch information
dajiaji committed Oct 13, 2024
1 parent 10fb299 commit ac79539
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
2 changes: 1 addition & 1 deletion npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions packages/common/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The base error class of hpke-js.
* @group Errors
*/
export class BaseError extends Error {
export class HpkeError extends Error {
public constructor(e: unknown) {
let message: string;

Expand All @@ -19,12 +19,6 @@ export class BaseError extends Error {
}
}

/**
* The base error class of hpke-js.
* @group Errors
*/
export class HpkeError extends BaseError {}

/**
* Invalid parameter.
* @group Errors
Expand Down
1 change: 0 additions & 1 deletion packages/core/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export type { CipherSuiteSealResponse } from "./src/interfaces/responses.ts";

export {
AeadId,
BaseError,
DecapError,
DeriveKeyPairError,
DeserializeError,
Expand Down
1 change: 0 additions & 1 deletion packages/hpke-js/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export type {

export {
AeadId,
BaseError,
DecapError,
DeriveKeyPairError,
DeserializeError,
Expand Down
19 changes: 16 additions & 3 deletions packages/hybridkem-x25519-kyber768/src/kyber/errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import { BaseError } from "@hpke/core";

/**
* The base error class of kyber.
*/
export class KyberError extends BaseError {}
export class KyberError extends Error {
public constructor(e: unknown) {
let message: string;

if (e instanceof Error) {
message = e.message;
} else if (typeof e === "string") {
message = e;
} else {
message = "";
}
super(message);

this.name = this.constructor.name;
}
}
38 changes: 38 additions & 0 deletions packages/hybridkem-x25519-kyber768/test/errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { assertEquals } from "@std/assert";

import { describe, it } from "@std/testing/bdd";

import { KyberError } from "../src/kyber/errors.ts";

describe("KyberError", () => {
describe("constructor with neigher string or Error", () => {
it("should have valid name and message", () => {
const err = new KyberError(undefined);

// assert
assertEquals(err.name, "KyberError");
assertEquals(err.message, "");
});
});

describe("constructor with string", () => {
it("should have valid name and message", () => {
const err = new KyberError("failed");

// assert
assertEquals(err.name, "KyberError");
assertEquals(err.message, "failed");
});
});

describe("constructor with another Error", () => {
it("should have valid name and message", () => {
const origin = new Error("failed");
const err = new KyberError(origin);

// assert
assertEquals(err.name, "KyberError");
assertEquals(err.message, "failed");
});
});
});

0 comments on commit ac79539

Please sign in to comment.