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

Refine errors. #437

Merged
merged 2 commits into from
Oct 13, 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
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.

20 changes: 14 additions & 6 deletions packages/common/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* 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 @@ -18,67 +19,74 @@ export class BaseError extends Error {
}
}

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

/**
* Invalid parameter.
* @group Errors
*/
export class InvalidParamError extends HpkeError {}

/**
* KEM input or output validation failure.
* @group Errors
*/
export class ValidationError extends HpkeError {}

/**
* Public or private key serialization failure.
* @group Errors
*/
export class SerializeError extends HpkeError {}

/**
* Public or private key deserialization failure.
* @group Errors
*/
export class DeserializeError extends HpkeError {}

/**
* encap() failure.
* @group Errors
*/
export class EncapError extends HpkeError {}

/**
* decap() failure.
* @group Errors
*/
export class DecapError extends HpkeError {}

/**
* Secret export failure.
* @group Errors
*/
export class ExportError extends HpkeError {}

/**
* seal() failure.
* @group Errors
*/
export class SealError extends HpkeError {}

/**
* open() failure.
* @group Errors
*/
export class OpenError extends HpkeError {}

/**
* Sequence number overflow on the encryption context.
* @group Errors
*/
export class MessageLimitReachedError extends HpkeError {}

/**
* Key pair derivation failure.
* @group Errors
*/
export class DeriveKeyPairError extends HpkeError {}

/**
* Not supported failure.
* @group Errors
*/
export class NotSupportedError extends HpkeError {}
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");
});
});
});
Loading