summaryrefslogtreecommitdiffstats
path: root/third_party/js/PKI.js/src/errors
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/js/PKI.js/src/errors')
-rw-r--r--third_party/js/PKI.js/src/errors/ArgumentError.ts76
-rw-r--r--third_party/js/PKI.js/src/errors/AsnError.ts31
-rw-r--r--third_party/js/PKI.js/src/errors/ParameterError.ts57
-rw-r--r--third_party/js/PKI.js/src/errors/index.ts3
4 files changed, 167 insertions, 0 deletions
diff --git a/third_party/js/PKI.js/src/errors/ArgumentError.ts b/third_party/js/PKI.js/src/errors/ArgumentError.ts
new file mode 100644
index 0000000000..c3852f6b93
--- /dev/null
+++ b/third_party/js/PKI.js/src/errors/ArgumentError.ts
@@ -0,0 +1,76 @@
+
+export interface AnyConstructor {
+ new(args: any): any;
+}
+
+export type ArgumentType =
+ | "undefined"
+ | "null"
+ | "boolean"
+ | "number"
+ | "string"
+ | "object"
+ | "Array"
+ | "ArrayBuffer"
+ | "ArrayBufferView"
+ | AnyConstructor;
+
+export class ArgumentError extends TypeError {
+
+ public static readonly NAME = "ArgumentError";
+
+ public static isType(value: any, type: "undefined"): value is undefined;
+ public static isType(value: any, type: "null"): value is null;
+ public static isType(value: any, type: "boolean"): value is boolean;
+ public static isType(value: any, type: "number"): value is number;
+ public static isType(value: any, type: "object"): value is object;
+ public static isType(value: any, type: "string"): value is string;
+ public static isType(value: any, type: "Array"): value is any[];
+ public static isType(value: any, type: "ArrayBuffer"): value is ArrayBuffer;
+ public static isType(value: any, type: "ArrayBufferView"): value is ArrayBufferView;
+ public static isType<T>(value: any, type: new (...args: any[]) => T): value is T;
+ // @internal
+ public static isType(value: any, type: ArgumentType): boolean;
+ public static isType(value: any, type: ArgumentType): boolean {
+ if (typeof type === "string") {
+ if (type === "Array" && Array.isArray(value)) {
+ return true;
+ } else if (type === "ArrayBuffer" && value instanceof ArrayBuffer) {
+ return true;
+ } else if (type === "ArrayBufferView" && ArrayBuffer.isView(value)) {
+ return true;
+ } else if (typeof value === type) {
+ return true;
+ }
+ } else if (value instanceof type) {
+ return true;
+ }
+
+ return false;
+ }
+
+ public static assert(value: any, name: string, type: "undefined"): asserts value is undefined;
+ public static assert(value: any, name: string, type: "null"): asserts value is null;
+ public static assert(value: any, name: string, type: "boolean"): asserts value is boolean;
+ public static assert(value: any, name: string, type: "number"): asserts value is number;
+ public static assert(value: any, name: string, type: "object"): asserts value is { [key: string]: any; };
+ public static assert(value: any, name: string, type: "string"): asserts value is string;
+ public static assert(value: any, name: string, type: "Array"): asserts value is any[];
+ public static assert(value: any, name: string, type: "ArrayBuffer"): asserts value is ArrayBuffer;
+ public static assert(value: any, name: string, type: "ArrayBufferView"): asserts value is ArrayBufferView;
+ public static assert<T>(value: any, name: string, type: new (...args: any[]) => T): asserts value is T;
+ public static assert(value: any, name: string, type: ArgumentType, ...types: ArgumentType[]): void;
+ public static assert(value: any, name: string, ...types: ArgumentType[]): void {
+ for (const type of types) {
+ if (this.isType(value, type)) {
+ return;
+ }
+ }
+
+ const typeNames = types.map(o => o instanceof Function && "name" in o ? o.name : `${o}`);
+ throw new ArgumentError(`Parameter '${name}' is not of type ${typeNames.length > 1 ? `(${typeNames.join(" or ")})` : typeNames[0]}`);
+ }
+
+ public override name: typeof ArgumentError.NAME = ArgumentError.NAME;
+
+}
diff --git a/third_party/js/PKI.js/src/errors/AsnError.ts b/third_party/js/PKI.js/src/errors/AsnError.ts
new file mode 100644
index 0000000000..60f159695b
--- /dev/null
+++ b/third_party/js/PKI.js/src/errors/AsnError.ts
@@ -0,0 +1,31 @@
+export interface AsnFromBerResult {
+ offset: number;
+ result: any;
+}
+
+export interface AsnCompareSchemaResult {
+ verified: boolean;
+ result?: any;
+}
+
+export class AsnError extends Error {
+
+ static assertSchema(asn1: AsnCompareSchemaResult, target: string): asserts asn1 is { verified: true, result: any; } {
+ if (!asn1.verified) {
+ throw new Error(`Object's schema was not verified against input data for ${target}`);
+ }
+ }
+
+ public static assert(asn: AsnFromBerResult, target: string): void {
+ if (asn.offset === -1) {
+ throw new AsnError(`Error during parsing of ASN.1 data. Data is not correct for '${target}'.`);
+ }
+ }
+
+ constructor(message: string) {
+ super(message);
+
+ this.name = "AsnError";
+ }
+
+} \ No newline at end of file
diff --git a/third_party/js/PKI.js/src/errors/ParameterError.ts b/third_party/js/PKI.js/src/errors/ParameterError.ts
new file mode 100644
index 0000000000..cf32fbd56a
--- /dev/null
+++ b/third_party/js/PKI.js/src/errors/ParameterError.ts
@@ -0,0 +1,57 @@
+import { EMPTY_STRING } from "../constants";
+import { ArgumentError } from "./ArgumentError";
+
+export class ParameterError extends TypeError {
+
+ public static readonly NAME = "ParameterError";
+
+ public static assert(target: string, params: any, ...fields: string[]): void;
+ public static assert(params: any, ...fields: string[]): void;
+ public static assert(...args: any[]): void {
+ let target: string | null = null;
+ let params: any;
+ let fields: string[];
+ if (typeof args[0] === "string") {
+ target = args[0];
+ params = args[1];
+ fields = args.slice(2);
+ } else {
+ params = args[0];
+ fields = args.slice(1);
+ }
+ ArgumentError.assert(params, "parameters", "object");
+ for (const field of fields) {
+ const value = params[field];
+ if (value === undefined || value === null) {
+ throw new ParameterError(field, target);
+ }
+ }
+ }
+
+ public static assertEmpty(value: unknown, name: string, target?: string): asserts value {
+ if (value === undefined || value === null) {
+ throw new ParameterError(name, target);
+ }
+ }
+
+ public override name: typeof ParameterError.NAME = ParameterError.NAME;
+
+ public field: string;
+ public target?: string;
+
+ constructor(field: string, target: string | null = null, message?: string) {
+ super();
+
+ this.field = field;
+ if (target) {
+ this.target = target;
+ }
+
+ if (message) {
+ this.message = message;
+ } else {
+ this.message = `Absent mandatory parameter '${field}' ${target ? ` in '${target}'` : EMPTY_STRING}`;
+ }
+ }
+
+} \ No newline at end of file
diff --git a/third_party/js/PKI.js/src/errors/index.ts b/third_party/js/PKI.js/src/errors/index.ts
new file mode 100644
index 0000000000..2e40b3f8f7
--- /dev/null
+++ b/third_party/js/PKI.js/src/errors/index.ts
@@ -0,0 +1,3 @@
+export * from "./ParameterError";
+export * from "./ArgumentError";
+export * from "./AsnError";