Skip to content

Commit cec662a

Browse files
authored
fix: when schema is not found in post config correct http code is returned (#50)
* fix: when schema is not found in post config correct http code is returned * test: removed console log
1 parent 4be6da0 commit cec662a

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/configs/controllers/configController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ConfigVersionMismatchError,
1616
SortQueryRepeatError,
1717
} from '../models/errors';
18+
import { SchemaNotFoundError } from '../../schemas/models/errors';
1819

1920
function configMapper(config: Config): components['schemas']['config'] {
2021
return {
@@ -113,7 +114,7 @@ export class ConfigController {
113114
await this.manager.createConfig(req.body);
114115
return res.status(httpStatus.CREATED).json();
115116
} catch (error) {
116-
if (error instanceof ConfigValidationError || error instanceof ConfigNotFoundError) {
117+
if (error instanceof ConfigValidationError || error instanceof ConfigNotFoundError || error instanceof SchemaNotFoundError) {
117118
(error as HttpError).status = httpStatus.BAD_REQUEST;
118119
} else if (error instanceof ConfigVersionMismatchError || error instanceof ConfigSchemaMismatchError) {
119120
(error as HttpError).status = httpStatus.CONFLICT;

src/serverBuilder.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ export class ServerBuilder {
7777

7878
private registerPostRoutesMiddleware(): void {
7979
const isStaticEnabled = this.config.get<boolean>('server.staticAssets.enabled');
80-
console.log('isStaticEnabled', isStaticEnabled);
8180

8281
if (isStaticEnabled) {
8382
const staticPath = this.config.get<string>('server.staticAssets.folder');

tests/integration/configs/configs.spec.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { Drizzle } from '../../../src/db/createConnection';
1414
import { getApp } from '../../../src/app';
1515
import { SERVICES } from '../../../src/common/constants';
1616
import { Config, configs, configsRefs } from '../../../src/configs/models/config';
17+
import { SchemaNotFoundError } from '../../../src/schemas/models/errors';
1718
import { ConfigRequestSender } from './helpers/requestSender';
1819
import { configsMockData, refs, schemaWithRef, simpleSchema, primitiveRefSchema, primitiveSchema } from './helpers/data';
1920

@@ -28,7 +29,7 @@ async function getSchemaMock(id: string): Promise<JSONSchema> {
2829
case primitiveRefSchema.$id:
2930
return Promise.resolve(primitiveRefSchema);
3031
default:
31-
throw new Error('Schema not found');
32+
throw new SchemaNotFoundError('Schema not found');
3233
}
3334
}
3435

@@ -536,6 +537,21 @@ describe('config', function () {
536537
expect(response).toSatisfyApiSpec();
537538
});
538539

540+
it('should return 400 if the schemaId of the config does not exist', async function () {
541+
const response = await requestSender.postConfig({
542+
configName: 'config-not-exists',
543+
schemaId: 'https://mapcolonies.com/not-exists/v1',
544+
version: 1,
545+
config: {
546+
manager: 'null',
547+
role: 'unknown',
548+
},
549+
});
550+
551+
expect(response.status).toBe(httpStatusCodes.BAD_REQUEST);
552+
expect(response).toSatisfyApiSpec();
553+
});
554+
539555
it('should return 409 status code when trying to post a new version of a config that does not exists', async function () {
540556
const response = await requestSender.postConfig({
541557
configName: 'not-exists',

0 commit comments

Comments
 (0)