/common/dto/output.dto.ts
import { Entity } from 'typeorm';
@Entity()
export class CoreOutput {
ok: boolean;
error?: string;
}
/products/output.dto.ts
import { CoreOutput } from 'src/common/dto/output.dto';
import { Entity } from 'typeorm';
import { Product } from '../entities/products.entity';
@Entity()
export class ProductOutput extends CoreOutput {
data?: Product[];
// getAll 객체 배열
}
@Entity()
export class ProductOutputOne extends CoreOutput {
data?: Product;
// getOne 단일 객체
}
async getAll(): Promise<ProductOutput> {
try {
// throw new error();
return {
ok: true,
data: await this.products.find(),
};
} catch (e) {
return {
ok: false,
error: e,
};
}
}
async getOne(id: number): Promise<ProductOutputOne> {
try {
// throw new error();
return {
ok: true,
data: await this.products.findOne(id),
};
} catch (e) {
return {
ok: false,
error: e,
};
}
}
- getAll, getOne 각각의 반환 타입으로 return
- create, update, delete는 반환 타입이 void
async getOne(id: number): Promise<ProductOutputOne> {
try {
throw new BadRequestException();
return {
ok: true,
data: await this.products.findOne(id),
};
} catch (e) {
return {
ok: false,
error: e,
};
}
}
위와 같이 BadRequestException을 던졌을 시
{
"ok": false,
"error": {
"response": {
"statusCode": 400,
"message": "Bad Request"
},
"status": 400,
"message": "Bad Request"
}
}
Error statusCode, message return