Skip to content

Commit

Permalink
refactor: use Intl.ListFormat for error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Oct 14, 2024
1 parent 590d3a0 commit 3c4bae7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ export class Container {
const token = injection
return token.name
})
assert(false, ErrorMessage.UnresolvableToken, tokenNames.join(', '))
const formatter = new Intl.ListFormat('en', {style: 'narrow', type: 'conjunction'})
assert(false, ErrorMessage.UnresolvableToken, formatter.format(tokenNames))
}

resolveProvider<Value>(token: InjectionToken<Value>): InjectionProvider<Value> | undefined {
Expand Down
9 changes: 5 additions & 4 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
export const ErrorMessage = {
ReservedToken: 'reserved token:',
UnresolvableToken: 'unresolvable token:',
CircularDependency: 'circular dependency:',
ReservedToken: 'reserved token',
UnresolvableToken: 'unresolvable token',
CircularDependency: 'circular dependency',
InvariantViolation: 'invariant violation',
InjectOutsideOfContext: 'inject outside of context',
} as const

export function assert(condition: unknown, ...args: any[]): asserts condition {
if (!condition) {
throw new Error(args.join(' '))
const formatter = new Intl.ListFormat('en', {style: 'narrow', type: 'unit'})
throw new Error(formatter.format(args))
}
}

Expand Down

0 comments on commit 3c4bae7

Please sign in to comment.