Fix unauthorized error handling (#6835)
from @BOHEUS comments in #6640 - fix bad 500 error when authentication invalid - remove "id", "createdAt", "updatedAt", etc from creation and update paths schema - improve error message - remove "id" from test body - improve secondaryLink schema description - improve depth parameter description - remove required from response body - improve examples - improve error message formatting - fix filter by position - answered to negative float position @BOHEUS comment Also: - fix secondary field openapi field description - remove schema display in playground Screenshots  
This commit is contained in:
@ -2,22 +2,34 @@ import { BadRequestException } from '@nestjs/common';
|
||||
|
||||
import { BaseGraphQLError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
|
||||
|
||||
const formatMessage = (message: BaseGraphQLError) => {
|
||||
if (message.extensions) {
|
||||
return message.extensions.response.message || message.extensions.response;
|
||||
const formatMessage = (error: BaseGraphQLError) => {
|
||||
let formattedMessage = error.extensions
|
||||
? error.extensions.response?.error ||
|
||||
error.extensions.response ||
|
||||
error.message
|
||||
: error.error;
|
||||
|
||||
formattedMessage = formattedMessage
|
||||
.replace(/"/g, "'")
|
||||
.replace("Variable '$data' got i", 'I')
|
||||
.replace("Variable '$input' got i", 'I');
|
||||
|
||||
const regex = /Field '[^']+' is not defined by type .*/;
|
||||
|
||||
const match = formattedMessage.match(regex);
|
||||
|
||||
if (match) {
|
||||
formattedMessage = match[0];
|
||||
}
|
||||
|
||||
return message.message;
|
||||
return formattedMessage;
|
||||
};
|
||||
|
||||
export class RestApiException extends BadRequestException {
|
||||
constructor(errors: BaseGraphQLError[]) {
|
||||
super({
|
||||
statusCode: 400,
|
||||
message:
|
||||
errors.length === 1
|
||||
? formatMessage(errors[0])
|
||||
: JSON.stringify(errors.map((error) => formatMessage(error))),
|
||||
messages: errors.map((error) => formatMessage(error)),
|
||||
error: 'Bad Request',
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user