Skip to content

Commit

Permalink
refactor: inline error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Oct 21, 2024
1 parent 56e1137 commit b8590ab
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 25 deletions.
2 changes: 0 additions & 2 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ export default tseslint.config(
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-namespace": "off",
"@typescript-eslint/no-unsafe-assignment": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-function-type": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/restrict-plus-operands": "off",
},
},
{
Expand Down
16 changes: 8 additions & 8 deletions src/container.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {assert, ErrorMessage, expectNever} from "./errors";
import {assert, expectNever} from "./errors";
import {useInjectionContext, withInjectionContext} from "./injection-context";
import {getMetadata, getRegistration} from "./metadata";
import {
Expand Down Expand Up @@ -123,7 +123,7 @@ export class Container {
return this.createInstance(registration);
}
}
this.throwUnregisteredError(tokens);
throwUnregisteredError(tokens);
}

resolveAll<Values extends unknown[]>(...tokens: TokenList<Values>): NonNullable<Values[number]>[];
Expand All @@ -146,7 +146,7 @@ export class Container {
return [this.createInstance(registration)];
}
}
this.throwUnregisteredError(tokens);
throwUnregisteredError(tokens);
}

private createInstance<T>(registration: Registration<T>): T {
Expand Down Expand Up @@ -185,7 +185,7 @@ export class Container {

if (context.resolution.stack.has(provider)) {
const dependentRef = context.resolution.dependents.get(provider);
assert(dependentRef, ErrorMessage.CircularDependency);
assert(dependentRef, "circular dependency detected");
return dependentRef.current;
}

Expand Down Expand Up @@ -227,9 +227,9 @@ export class Container {
context.resolution.stack.pop();
}
}
}

private throwUnregisteredError(tokens: Token[]): never {
const tokenNames = tokens.map((token) => token.name);
assert(false, ErrorMessage.UnregisteredToken, tokenNames.join(", "));
}
function throwUnregisteredError(tokens: Token[]): never {
const tokenNames = tokens.map((token) => token.name);
assert(false, `unregistered token ${tokenNames.join(", ")}`);
}
14 changes: 4 additions & 10 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
export const ErrorMessage = {
ReservedToken: "reserved token",
UnregisteredToken: "unregistered token",
CircularDependency: "circular dependency",
MissingInjectionContext: "missing injection context",
} as const;

// @internal
export function assert(condition: unknown, ...args: any[]): asserts condition {
export function assert(condition: unknown, message: string): asserts condition {
if (!condition) {
throw new Error(args.join(" "));
throw new Error(message);
}
}

// @internal
export function expectNever(value: never): never {
throw new TypeError("unexpected value: " + value);
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
throw new TypeError(`unexpected value ${value}`);
}
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export type {ContainerOptions} from "./container";
export {Container} from "./container";
export type {ClassDecorator, ClassFieldDecorator, ClassFieldInitializer} from "./decorators";
export {AutoRegister, Inject, Injectable, InjectAll, Scoped} from "./decorators";
export {ErrorMessage} from "./errors";
export {inject, injectAll} from "./inject";
export type {InstanceRef} from "./instance";
export type {ClassProvider, FactoryProvider, Provider, ValueProvider} from "./provider";
Expand Down
4 changes: 2 additions & 2 deletions src/injection-context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {Container} from "./container";
import {assert, ErrorMessage} from "./errors";
import {assert} from "./errors";
import type {InstanceRef} from "./instance";
import type {Provider} from "./provider";
import type {Scope} from "./scope";
Expand Down Expand Up @@ -30,6 +30,6 @@ export const [withInjectionContext, useInjectionContext] = createContext<Injecti
// @internal
export function ensureInjectionContext(fn: Function) {
const context = useInjectionContext();
assert(context, fn.name, ErrorMessage.MissingInjectionContext);
assert(context, `${fn.name}() can only be used within an injection context`);
return context;
}
3 changes: 1 addition & 2 deletions src/registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {assert} from "./errors";
import {ErrorMessage} from "./errors";
import type {InstanceRef} from "./instance";
import {NullProvider, type Provider, UndefinedProvider} from "./provider";
import {Scope} from "./scope";
Expand Down Expand Up @@ -78,7 +77,7 @@ export class Registry {
}

set<T>(token: Token<T>, registration: Registration<T>): void {
assert(!internals.has(token), ErrorMessage.ReservedToken, token.name);
assert(!internals.has(token), `cannot register reserved token ${token.name}`);
let registrations = this._map.get(token);
if (!registrations) {
registrations = [];
Expand Down

0 comments on commit b8590ab

Please sign in to comment.