Skip to content

Commit

Permalink
Move shared error classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Mar 24, 2024
1 parent f439421 commit 53b6eef
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
21 changes: 21 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { quote } from "./shell.js";

export function normalize(error: unknown): Error {
return error instanceof Error ? error : new Error(String(error));
}
Expand All @@ -10,3 +12,22 @@ export class SpecError extends Error {
super(`specification for ${name} is invalid: ${cause.message}`);
}
}

export class ValueError extends Error {
constructor(
public readonly name: string,
public readonly isSensitive: boolean,
public readonly value: string,
public readonly cause: Error,
) {
const renderedValue = isSensitive ? "<sensitive value>" : quote(value);

super(`value of ${name} (${renderedValue}) is invalid: ${cause.message}`);
}
}

export class NotSetError extends Error {
constructor(public readonly name: string) {
super(`${name} is not set and does not have a default value`);
}
}
3 changes: 2 additions & 1 deletion src/summary.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ValueError } from "./error.js";
import { Visitor } from "./schema.js";
import { quote } from "./shell.js";
import { create as createTable } from "./table.js";
import { Result, Results } from "./validation.js";
import { ValueError, Variable } from "./variable.js";
import { Variable } from "./variable.js";

const ATTENTION = "❯";
const INVALID = "✗";
Expand Down
22 changes: 1 addition & 21 deletions src/variable.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { applyConstraints } from "./constraint.js";
import { readVariable } from "./environment.js";
import { SpecError, normalize } from "./error.js";
import { NotSetError, SpecError, ValueError, normalize } from "./error.js";
import { type Example } from "./example.js";
import { Maybe, definedValue, map, undefinedValue } from "./maybe.js";
import { Schema } from "./schema.js";
import { quote } from "./shell.js";

export type VariableSpec<T> = {
readonly name: string;
Expand Down Expand Up @@ -141,22 +140,3 @@ export function create<T>(spec: VariableSpec<T>): Variable<T> {
}
}
}

export class ValueError extends Error {
constructor(
public readonly name: string,
public readonly isSensitive: boolean,
public readonly value: string,
public readonly cause: Error,
) {
const renderedValue = isSensitive ? "<sensitive value>" : quote(value);

super(`value of ${name} (${renderedValue}) is invalid: ${cause.message}`);
}
}

export class NotSetError extends Error {
constructor(public readonly name: string) {
super(`${name} is not set and does not have a default value`);
}
}
2 changes: 1 addition & 1 deletion test/suite/initialize.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { Declaration, Options } from "../../src/declaration.js";
import { NotSetError } from "../../src/error.js";
import { OnInvalid, boolean, initialize, string } from "../../src/index.js";
import { Results } from "../../src/validation.js";
import { NotSetError } from "../../src/variable.js";
import { MockConsole, Mocked, createMockConsole, mockFn } from "../helpers.js";

describe("initialize()", () => {
Expand Down

0 comments on commit 53b6eef

Please sign in to comment.