Skip to content

Commit

Permalink
refactor: http error
Browse files Browse the repository at this point in the history
  • Loading branch information
jannyHou committed Jun 12, 2018
1 parent bdf9089 commit 9f599cb
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
7 changes: 3 additions & 4 deletions packages/rest/src/coercion/coerce-parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import {ParameterObject, isReferenceObject} from '@loopback/openapi-v3-types';
import {Validator} from './validator';
import * as debugModule from 'debug';
import {HttpErrorMessage} from '../';
import {RestHttpError} from '../';

const debug = debugModule('loopback:rest:coercion');

Expand Down Expand Up @@ -43,9 +43,8 @@ export function coerceParameter(data: string, spec: ParameterObject) {
case 'number':
const coercedData = data ? Number(data) : undefined;
if (coercedData === undefined) return;
if (isNaN(coercedData))
throw HttpErrorMessage.invalidData(data, spec.name);
return coercedData;
if (isNaN(coercedData)) throw RestHttpError.invalidData(data, spec.name);
return coercedData;
case 'long':
return Number(data);
case 'integer':
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as HttpErrors from 'http-errors';
export namespace HttpErrorMessage {
export namespace RestHttpError {
export function invalidData<T>(data: T, name: string) {
const msg = `Invalid data ${JSON.stringify(data)} for parameter ${name}!`;
return new HttpErrors.BadRequest(msg);
Expand Down
4 changes: 2 additions & 2 deletions packages/rest/src/coercion/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// License text available at https://opensource.org/licenses/MIT

import {ParameterObject} from '@loopback/openapi-v3-types';
import {HttpErrorMessage} from '../';
import {RestHttpError} from '../';

/**
* A set of options to pass into the validator functions
Expand Down Expand Up @@ -42,7 +42,7 @@ export class Validator {
if (this.isAbsent(value)) {
if (this.isRequired(opts)) {
const name = this.ctx.parameterSpec.name;
throw HttpErrorMessage.missingRequired(name);
throw RestHttpError.missingRequired(name);
} else {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ export * from './rest.component';
export * from './rest.server';
export * from './sequence';
export * from '@loopback/openapi-v3';
export * from './coercion/http-error-message';
export * from './coercion/rest-http-error';
10 changes: 5 additions & 5 deletions packages/rest/test/unit/coercion/paramStringToNumber.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import {test} from './utils';
import {ParameterLocation} from '@loopback/openapi-v3-types';
import * as HttpErrors from 'http-errors';
import {HttpErrorMessage} from './../../../';
import {RestHttpError} from './../../../';

const NUMBER_PARAM = {
in: <ParameterLocation>'path',
Expand Down Expand Up @@ -48,10 +48,10 @@ describe('coerce param from string to number - required', () => {

context('empty values trigger ERROR_BAD_REQUEST', () => {
// null, '' sent from request are converted to raw value ''
test<HttpErrors.HttpError>(
test(
REQUIRED_NUMBER_PARAM,
'',
HttpErrorMessage.missingRequired(REQUIRED_NUMBER_PARAM.name),
RestHttpError.missingRequired(REQUIRED_NUMBER_PARAM.name),
);
});
});
Expand Down Expand Up @@ -85,13 +85,13 @@ describe('coerce param from string to number - optional', () => {
test(
NUMBER_PARAM,
'text',
HttpErrorMessage.invalidData('text', NUMBER_PARAM.name),
RestHttpError.invalidData('text', NUMBER_PARAM.name),
);
// {a: true}, [1,2] are converted to object
test(
NUMBER_PARAM,
{a: true},
HttpErrorMessage.invalidData({a: true}, NUMBER_PARAM.name),
RestHttpError.invalidData({a: true}, NUMBER_PARAM.name),
);
});
});
Expand Down

0 comments on commit 9f599cb

Please sign in to comment.