From ac79539f515fa047d62f3b2583d31fd8067b94e3 Mon Sep 17 00:00:00 2001 From: Ajitomi Daisuke Date: Mon, 14 Oct 2024 08:00:16 +0900 Subject: [PATCH] Remove BaseError. --- npm/package-lock.json | 2 +- packages/common/src/errors.ts | 8 +--- packages/core/mod.ts | 1 - packages/hpke-js/mod.ts | 1 - .../src/kyber/errors.ts | 19 ++++++++-- .../test/errors.test.ts | 38 +++++++++++++++++++ 6 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 packages/hybridkem-x25519-kyber768/test/errors.test.ts diff --git a/npm/package-lock.json b/npm/package-lock.json index 9868fce4f..6809f99ec 100644 --- a/npm/package-lock.json +++ b/npm/package-lock.json @@ -279,12 +279,12 @@ "license": "MIT", "dependencies": { "@hpke/common": "^1.4.3", - "@hpke/core": "^1.4.3", "@hpke/dhkem-x25519": "^1.4.3", "@noble/hashes": "^1.4.0" }, "devDependencies": { "@deno/shim-deno": "~0.18.0", + "@hpke/core": "^1.4.3", "@types/node": "^20.9.0", "picocolors": "^1.0.0" }, diff --git a/packages/common/src/errors.ts b/packages/common/src/errors.ts index eb3271a59..e1516bad8 100644 --- a/packages/common/src/errors.ts +++ b/packages/common/src/errors.ts @@ -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; @@ -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 diff --git a/packages/core/mod.ts b/packages/core/mod.ts index 96e9c4ab6..c78baefb8 100644 --- a/packages/core/mod.ts +++ b/packages/core/mod.ts @@ -18,7 +18,6 @@ export type { CipherSuiteSealResponse } from "./src/interfaces/responses.ts"; export { AeadId, - BaseError, DecapError, DeriveKeyPairError, DeserializeError, diff --git a/packages/hpke-js/mod.ts b/packages/hpke-js/mod.ts index 3403797e3..3a23a4b0d 100644 --- a/packages/hpke-js/mod.ts +++ b/packages/hpke-js/mod.ts @@ -15,7 +15,6 @@ export type { export { AeadId, - BaseError, DecapError, DeriveKeyPairError, DeserializeError, diff --git a/packages/hybridkem-x25519-kyber768/src/kyber/errors.ts b/packages/hybridkem-x25519-kyber768/src/kyber/errors.ts index f89987563..99a048d93 100644 --- a/packages/hybridkem-x25519-kyber768/src/kyber/errors.ts +++ b/packages/hybridkem-x25519-kyber768/src/kyber/errors.ts @@ -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; + } +} diff --git a/packages/hybridkem-x25519-kyber768/test/errors.test.ts b/packages/hybridkem-x25519-kyber768/test/errors.test.ts new file mode 100644 index 000000000..d7faac912 --- /dev/null +++ b/packages/hybridkem-x25519-kyber768/test/errors.test.ts @@ -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"); + }); + }); +});