Skip to content

Commit

Permalink
Migrate status based Error creation from inline check to HttpError.cr…
Browse files Browse the repository at this point in the history
…eate method
  • Loading branch information
efabris committed May 22, 2020
1 parent 27a4bd0 commit 36118ea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/middlewares/openapi.multipart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
BadRequest,
RequestEntityToLarge,
InternalServerError,
HttpError,
} from '../framework/types';
import { MulterError } from 'multer';
import ajv = require('ajv');
Expand Down Expand Up @@ -117,11 +118,16 @@ function error(req: OpenApiRequest, err: Error): ValidationError {
);
const unexpected = /LIMIT_UNEXPECTED_FILE/.test(multerError.code);
const status = payload_too_big ? 413 : !unexpected ? 400 : 500;
return payload_too_big
return HttpError.create({
status: status,
path: req.path,
message: err.message,
});
/*return payload_too_big
? new RequestEntityToLarge({ path: req.path, message: err.message })
: !unexpected
? new BadRequest({ path: req.path, message: err.message })
: new InternalServerError({ path: req.path, message: err.message });
: new InternalServerError({ path: req.path, message: err.message });*/
} else {
// HACK
// TODO improve multer error handling
Expand Down
10 changes: 8 additions & 2 deletions src/middlewares/openapi.security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
InternalServerError,
Unauthorized,
Forbidden,
HttpError,
} from '../framework/types';
import { OpenApiContext } from '../framework/openapi.context';

Expand Down Expand Up @@ -105,12 +106,17 @@ export function security(
}
} catch (e) {
const message = e?.error?.message || 'unauthorized';
const err =
const err = HttpError.create({
status: e.status,
path: path,
message: message,
});
/*const err =
e.status == 500
? new InternalServerError({ path: path, message: message })
: e.status == 403
? new Forbidden({ path: path, message: message })
: new Unauthorized({ path: path, message: message });
: new Unauthorized({ path: path, message: message });*/
next(err);
}
};
Expand Down

0 comments on commit 36118ea

Please sign in to comment.