HM-35. Описаны все ответы ошибок сервера, поправлены ответы и тексты описания. HM-60. Исправлена ошибка падения сервера при попытке найти не существующее хранилище

This commit is contained in:
vigdorov
2020-07-31 22:01:42 +03:00
parent 0c9ae5d2cf
commit 0aa73513c1
5 changed files with 161 additions and 61 deletions

View File

@ -1,5 +1,5 @@
import {Model, Connection} from 'mongoose';
import {Injectable, NotFoundException, BadGatewayException} from '@nestjs/common';
import {Injectable, NotFoundException, BadGatewayException, ConflictException, BadRequestException} from '@nestjs/common';
import {InjectConnection} from '@nestjs/mongoose';
import {Store, StoreRequest, StoreSchema} from './store.schema';
import {DB_TEST_NAME, DB_NAME, COLLECTION_STORE} from 'src/consts';
@ -34,12 +34,20 @@ export class StoreService {
const searchStore = await this.findOne(api, store.key);
if (searchStore) {
throw new NotFoundException(`Api key ${store.key} is already taken`);
throw new ConflictException(`Api key "${store.key}" is already taken`);
}
const createdStore = new (this.storeModel(api))(store);
await validateModel(createdStore);
try {
await validateModel(createdStore);
} catch (e) {
if (e?.message?.includes('validation failed')) {
throw new BadRequestException(e.message);
}
throw e;
}
const savedStore = await createdStore.save();
if (!Object.keys(savedStore.value).length) {
@ -57,7 +65,14 @@ export class StoreService {
author: searchStore.author,
};
const updateStore = new (this.storeModel(api))(store);
await validateModel(updateStore);
try {
await validateModel(updateStore);
} catch (e) {
if (e?.message?.includes('validation failed')) {
throw new BadRequestException(e.message);
}
throw e;
}
await searchStore.updateOne({
...store,
@ -67,11 +82,14 @@ export class StoreService {
return updateStore;
}
throw new NotFoundException(`Not Found api key - ${omitProps.key}`);
throw new NotFoundException(`Not found api key "${omitProps.key}"`);
}
async findOne(api: string, key: string): Promise<Store> {
const searchStore = await this.storeModel(api).findOne({key});
if (!searchStore) {
throw new NotFoundException(`Not found api key "${key}"`);
}
return searchStore;
}
@ -84,6 +102,6 @@ export class StoreService {
return searchStore;
}
throw new NotFoundException(`Not Found key - ${key}`);
throw new NotFoundException(`Not found api key "${key}"`);
}
}