feat(proejct): project curd.
This commit is contained in:
6
src/commons/exceptions/Insufficient-balance.exception.ts
Normal file
6
src/commons/exceptions/Insufficient-balance.exception.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export class InsufficientBalanceException extends Error {
|
||||
constructor(curr: number, amount: number) {
|
||||
super();
|
||||
this.message = `余额不足。current balance:${curr},change amount:${amount}`;
|
||||
}
|
||||
}
|
21
src/commons/exceptions/application.exception.ts
Normal file
21
src/commons/exceptions/application.exception.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
export class ApplicationException extends Error {
|
||||
code: number;
|
||||
error: Error;
|
||||
|
||||
constructor(
|
||||
message:
|
||||
| string
|
||||
| { error?: Error; message?: string | object; code?: number },
|
||||
) {
|
||||
if (message instanceof Object) {
|
||||
super();
|
||||
this.code = message.code;
|
||||
this.error = message.error;
|
||||
this.message = message.message as any;
|
||||
} else if (typeof message === 'string') {
|
||||
super((message as unknown) as any);
|
||||
} else {
|
||||
super((message as unknown) as any);
|
||||
}
|
||||
}
|
||||
}
|
35
src/commons/exceptions/duplicate-entity.exception.ts
Normal file
35
src/commons/exceptions/duplicate-entity.exception.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { ValidationError } from '@nestjs/common';
|
||||
import { WrongContentException } from './wrong-content.exception';
|
||||
|
||||
export interface DuplicateFieldInfo {
|
||||
property: string;
|
||||
value: any;
|
||||
name?: string;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
export class DuplicateEntityException extends WrongContentException {
|
||||
errorObj: { [p: string]: any; message: ValidationError[] };
|
||||
constructor(exceptionInfo: DuplicateFieldInfo[]) {
|
||||
super(DuplicateEntityException.getValidationError(exceptionInfo));
|
||||
}
|
||||
|
||||
private static getValidationError(
|
||||
exceptionInfo: DuplicateFieldInfo[],
|
||||
): ValidationError[] {
|
||||
return exceptionInfo.map((item) => {
|
||||
const property: string = item.property;
|
||||
const value: any = item.value;
|
||||
const message: string = item.message
|
||||
? item.message
|
||||
: `${item.name || ''}已存在相同的值「${item.value}」`;
|
||||
return {
|
||||
property,
|
||||
value,
|
||||
constraints: { duplicate: message },
|
||||
target: undefined,
|
||||
children: [],
|
||||
} as ValidationError;
|
||||
});
|
||||
}
|
||||
}
|
6
src/commons/exceptions/lock-failed.exception.ts
Normal file
6
src/commons/exceptions/lock-failed.exception.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export class LockFailedException extends Error {
|
||||
constructor(lockId: any) {
|
||||
super();
|
||||
this.message = `加锁失败。目标:${lockId}`;
|
||||
}
|
||||
}
|
36
src/commons/exceptions/value-out-of-range.exception.ts
Normal file
36
src/commons/exceptions/value-out-of-range.exception.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { ValidationError } from '@nestjs/common';
|
||||
import { WrongContentException } from './wrong-content.exception';
|
||||
|
||||
export interface ValueOutOfRangeInfo {
|
||||
property: string;
|
||||
value: any;
|
||||
range: string;
|
||||
name?: string;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
export class ValueOutOfRangeException extends WrongContentException {
|
||||
errorObj: { [p: string]: any; message: ValidationError[] };
|
||||
constructor(exceptionInfo: ValueOutOfRangeInfo[]) {
|
||||
super(ValueOutOfRangeException.getValidationError(exceptionInfo));
|
||||
}
|
||||
|
||||
private static getValidationError(
|
||||
exceptionInfo: ValueOutOfRangeInfo[],
|
||||
): ValidationError[] {
|
||||
return exceptionInfo.map((item) => {
|
||||
const property: string = item.property;
|
||||
const value: any = item.value;
|
||||
const message: string = item.message
|
||||
? item.message
|
||||
: `${item.name || ''}的取值范围应是${item.range}`;
|
||||
return {
|
||||
property,
|
||||
value,
|
||||
constraints: { duplicate: message },
|
||||
target: undefined,
|
||||
children: [],
|
||||
} as ValidationError;
|
||||
});
|
||||
}
|
||||
}
|
7
src/commons/exceptions/wrong-content.exception.ts
Normal file
7
src/commons/exceptions/wrong-content.exception.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { UnprocessableEntityException, ValidationError } from '@nestjs/common';
|
||||
|
||||
export class WrongContentException extends UnprocessableEntityException {
|
||||
constructor(exceptionInfo: ValidationError[]) {
|
||||
super(exceptionInfo);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user