Skip to content

Commit

Permalink
优化抛出异常的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
kaokei committed Jan 13, 2025
1 parent c2a280b commit 81b4d92
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 84 deletions.
4 changes: 2 additions & 2 deletions src/binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import { Container } from './container';
import { getMetadata, getOwnMetadata } from './cachemap';
import { resolveToken } from './token';
import { CircularDependencyError } from './errors';
import { CircularDependencyError, BindingNotValidError } from './errors';

export class Binding {
public container!: Container;
Expand Down Expand Up @@ -108,7 +108,7 @@ export class Binding {
} else if (BindingTypeEnum.DynamicValue === this.type) {
return this.resolveDynamicValue();
} else {
// throw error
throw new BindingNotValidError(this);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/container.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Binding } from './binding';
import { TokenNotFoundError } from './errors/TokenNotFoundError';
import { TokenNotFoundError, DuplicateBindingError } from './errors';
import { IDENTITY, NOOP } from './constants';
import { GenericToken } from './interfaces';

Expand All @@ -11,7 +11,7 @@ export class Container {

public bind(serviceIdentifier: any) {
if (this.bindings.has(serviceIdentifier)) {
// todo: throw new Error('Already bound');
throw new DuplicateBindingError(serviceIdentifier);
}
const binding = this.buildBinding(serviceIdentifier);
this.bindings.set(serviceIdentifier, binding);
Expand Down
10 changes: 10 additions & 0 deletions src/errors/BindingNotValidError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export class BindingNotValidError extends Error {
public name = 'BINDING_NOT_VALID_ERROR';
public message = this.name;

constructor(binding: any) {
super();

this.message = `BINDING NOT VALID. YOU CAN USE ONE OF THESE METHODS {to/toSelf/toService/toConstantValue/toDynamicValue}. ${binding}`;
}
}
10 changes: 0 additions & 10 deletions src/errors/ConstructorInjectMissTokenError.ts

This file was deleted.

10 changes: 10 additions & 0 deletions src/errors/DuplicateBindingError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export class DuplicateBindingError extends Error {
public name = 'DUPLICATE_BINDING_ERROR';
public message = this.name;

constructor(token: any) {
super();

this.message = `DUPLICATE BINDING. ${token}`;
}
}
29 changes: 0 additions & 29 deletions src/errors/InjectFailedError.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/errors/PropertyInjectMissTokenError.ts

This file was deleted.

10 changes: 0 additions & 10 deletions src/errors/ProviderNotValidError.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/errors/TokenNotFoundError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export class TokenNotFoundError extends Error {
constructor(token: any) {
super();

this.message = `TOKEN IS NOT A INJECTABLE CLASS OR SKIP OUT OF ROOT INJECTOR. YOU CAN USE @Optional DECORATOR TO IGNORE THIS ERROR IF THIS SERVICE IS OPTIONAL. ${token}`;
this.message = `TOKEN IS NOT BOUND TO CONTAINER. YOU CAN USE @Optional DECORATOR TO IGNORE THIS ERROR IF THIS SERVICE IS OPTIONAL. ${token}`;
}
}
8 changes: 3 additions & 5 deletions src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export { CircularDependencyError } from './CircularDependencyError';
export { InjectFailedError } from './InjectFailedError';
export { ProviderNotValidError } from './ProviderNotValidError';
export { TokenNotFoundError } from './TokenNotFoundError';
export { ConstructorInjectMissTokenError } from './ConstructorInjectMissTokenError';
export { PropertyInjectMissTokenError } from './PropertyInjectMissTokenError';
export { BindingNotValidError } from './BindingNotValidError';
export { DuplicateBindingError } from './DuplicateBindingError';
export { CircularDependencyError } from './CircularDependencyError';
11 changes: 0 additions & 11 deletions src/test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true
// "noUncheckedSideEffectImports": true
},
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.ignore.ts"]
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true
// "noUncheckedSideEffectImports": true
},
"include": ["vite.config.ts"]
}
2 changes: 1 addition & 1 deletion tsconfig.vitest.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedSideEffectImports": true
// "noUncheckedSideEffectImports": true
},
"include": ["tests/**/*.ts"]
}
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import dts from 'vite-plugin-dts';
export default defineConfig({
plugins: [
dts({
rollupTypes: true,
// rollupTypes: true,
include: ["./src"],
tsconfigPath: './tsconfig.app.json',
beforeWriteFile: (filePath, content) => {
Expand Down

0 comments on commit 81b4d92

Please sign in to comment.