feat(proejct): project curd.

This commit is contained in:
Ivan Li
2021-01-31 19:42:17 +08:00
parent 7ba5e220d9
commit db6b699663
27 changed files with 1326 additions and 5 deletions

View File

@@ -0,0 +1,6 @@
export class InsufficientBalanceException extends Error {
constructor(curr: number, amount: number) {
super();
this.message = `余额不足。current balance${curr}change amount${amount}`;
}
}

View 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);
}
}
}

View 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;
});
}
}

View File

@@ -0,0 +1,6 @@
export class LockFailedException extends Error {
constructor(lockId: any) {
super();
this.message = `加锁失败。目标:${lockId}`;
}
}

View 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;
});
}
}

View File

@@ -0,0 +1,7 @@
import { UnprocessableEntityException, ValidationError } from '@nestjs/common';
export class WrongContentException extends UnprocessableEntityException {
constructor(exceptionInfo: ValidationError[]) {
super(exceptionInfo);
}
}