Skip to content

Commit

Permalink
6.29. Рефакторит контроллер CategoryController.
Browse files Browse the repository at this point in the history
Обновим метод `create` в контроллере `CategoryController`. Вместо подготовки ошибки вручную, подготовим `HttpError`.
  • Loading branch information
AntonovIgor committed Sep 10, 2024
1 parent e878a3c commit cc55800
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/shared/modules/category/category.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { inject, injectable } from 'inversify';
import { Request, Response } from 'express';
import { StatusCodes } from 'http-status-codes';

import { BaseController, HttpMethod } from '../../libs/rest/index.js';
import { BaseController, HttpError, HttpMethod } from '../../libs/rest/index.js';
import { Logger } from '../../libs/logger/index.js';
import { Component } from '../../types/index.js';
import { CategoryService } from './category-service.interface.js';
Expand Down Expand Up @@ -38,13 +38,11 @@ export class CategoryController extends BaseController {
const existCategory = await this.categoryService.findByCategoryName(body.name);

if (existCategory) {
const existCategoryError = new Error(`Category with name «${body.name}» exists.`);
this.send(res,
throw new HttpError(
StatusCodes.UNPROCESSABLE_ENTITY,
{ error: existCategoryError.message }
`Category with name «${body.name}» exists.`,
'CategoryController'
);

return this.logger.error(existCategoryError.message, existCategoryError);
}

const result = await this.categoryService.create(body);
Expand Down

0 comments on commit cc55800

Please sign in to comment.