Skip to content

Commit

Permalink
feat(exceptions): add better handling of validation errors
Browse files Browse the repository at this point in the history
cleaned up display of validation errors, added the exception factory correctly to the dispatch-api
  • Loading branch information
bilalshaikh42 committed Aug 18, 2021
1 parent d056b4a commit e4e1986
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
26 changes: 15 additions & 11 deletions apps/dispatch-api/src/main.ts
Expand Up @@ -55,20 +55,24 @@ async function bootstrap() {
const configService = app.get(ConfigService);
const limit = configService.get('server.limit');
app.use(json({ limit }));
app.useGlobalPipes(new ValidationPipe({ transform: true }));

setupOpenApi(app);
await app.listen(port, () => {
logger.log('Listening at http://localhost:' + port);
});


app.useGlobalPipes(
new ValidationPipe({
exceptionFactory: BiosimulationsValidationExceptionFactory,
transform: true,
transformOptions: {
enableImplicitConversion: true,
}
}),
);
app.enableVersioning({
type: VersioningType.URI,
});
}

);
app.enableVersioning({
type: VersioningType.URI,
});
await app.listen(port, () => {
logger.log('Listening at http://localhost:' + port);
});

}
bootstrap();
Expand Up @@ -21,13 +21,12 @@ import {
} from './simulation-run.model';
import { SimulationRunService } from './simulation-run.service';
import { SharedNatsClientModule } from '@biosimulations/shared/nats-client';
import { SharedExceptionsFiltersModule } from '@biosimulations/shared/exceptions/filters';

@Module({
controllers: [SimulationRunController],
imports: [
BiosimulationsAuthModule,
SharedExceptionsFiltersModule,

SharedNatsClientModule,
HttpModule,
SharedNatsClientModule,
Expand Down
2 changes: 2 additions & 0 deletions libs/shared/exceptions/exceptions/src/lib/exception.ts
Expand Up @@ -5,6 +5,8 @@ import {
} from '@biosimulations/datamodel/api';
import { HttpException } from '@nestjs/common';


// TODO refactor to be able to hold multiple errors
export class BiosimulationsException extends Error {
private errorObject: ErrorObject;
constructor(
Expand Down
Expand Up @@ -4,9 +4,11 @@ import { BiosimulationsException } from './exception';
export const BiosimulationsValidationExceptionFactory = (
errors: ValidationError[],
): BiosimulationsException => {
const err = errors[0];
const message = err.property + 'failed validation';

// TODO handle all errors, not just first one
const err = errors[0];
const message = 'Parameter or property "'+err.property + '" failed validation';

const bioSimErr = new BiosimulationsException(
HttpStatus.BAD_REQUEST,
'Validation Error',
Expand All @@ -15,7 +17,7 @@ export const BiosimulationsValidationExceptionFactory = (
undefined,
err.property,
undefined,
{ errors: errors },
{...err },
);

return bioSimErr;
Expand Down
Expand Up @@ -15,13 +15,14 @@ export class HttpExceptionFilter implements ExceptionFilter {
public catch(exception: HttpException, host: ArgumentsHost): void {
const ctx = host.switchToHttp();
const request = ctx.getRequest<Request>();
this.logger.error(exception);
this.logger.error(request.url);



this.logger.error( exception.getResponse(),request.url);
const response = ctx.getResponse<Response>();

const status = exception.getStatus();
const err = makeErrorObjectFromHttp(exception);
this.logger.log(err);
response.status(status).json({ error: [err] });
}
}
7 changes: 5 additions & 2 deletions libs/shared/exceptions/filters/src/lib/utils.ts
Expand Up @@ -37,7 +37,10 @@ export const makeErrorObject = (
export const makeErrorObjectFromHttp = (exception: HttpException) => {
let resp = exception.getResponse();
if (typeof resp !== 'string') {
resp = (resp as any)?.error as string;
resp = JSON.stringify((resp as any)?.message)
if (!resp) {
resp = (resp as any)?.error as string;
}
}
return makeErrorObject(exception.getStatus(), resp, exception.message);
return makeErrorObject(exception.getStatus(), exception.message, resp);
};

0 comments on commit e4e1986

Please sign in to comment.