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

![image](https://github.com/user-attachments/assets/a5d52afd-ab10-49f3-8806-ee41b04bc775)

![image](https://github.com/user-attachments/assets/33f985bb-ff75-42f6-a0bb-741bd32a1d08)
This commit is contained in:
martmull
2024-09-04 17:25:59 +02:00
committed by GitHub
parent c1eae56bb6
commit c55dfbde6e
16 changed files with 863 additions and 340 deletions

View File

@ -156,7 +156,23 @@ const fieldRatingMock = {
name: 'fieldRating',
type: FieldMetadataType.RATING,
isNullable: true,
defaultValue: null,
defaultValue: 'RATING_1',
options: [
{
id: '9a519a86-422b-4598-88ae-78751353f683',
color: 'red',
label: 'Opt 1',
value: 'RATING_1',
position: 0,
},
{
id: '33f28d51-bc82-4e1d-ae4b-d9e4c0ed0ab4',
color: 'purple',
label: 'Opt 2',
value: 'RATING_2',
position: 1,
},
],
};
const fieldPositionMock = {

View File

@ -1,11 +1,13 @@
import { Controller, Post, Req, Res } from '@nestjs/common';
import { Controller, Post, Req, Res, UseGuards } from '@nestjs/common';
import { Request, Response } from 'express';
import { RestApiCoreService } from 'src/engine/api/rest/core/rest-api-core.service';
import { cleanGraphQLResponse } from 'src/engine/api/rest/utils/clean-graphql-response.utils';
import { JwtAuthGuard } from 'src/engine/guards/jwt.auth.guard';
@Controller('rest/batch/*')
@UseGuards(JwtAuthGuard)
export class RestApiCoreBatchController {
constructor(private readonly restApiCoreService: RestApiCoreService) {}

View File

@ -7,14 +7,17 @@ import {
Put,
Req,
Res,
UseGuards,
} from '@nestjs/common';
import { Request, Response } from 'express';
import { RestApiCoreService } from 'src/engine/api/rest/core/rest-api-core.service';
import { cleanGraphQLResponse } from 'src/engine/api/rest/utils/clean-graphql-response.utils';
import { JwtAuthGuard } from 'src/engine/guards/jwt.auth.guard';
@Controller('rest/*')
@UseGuards(JwtAuthGuard)
export class RestApiCoreController {
constructor(private readonly restApiCoreService: RestApiCoreService) {}

View File

@ -23,12 +23,16 @@ export const formatFieldValue = (
if (comparator === 'is') {
return value;
}
if (fieldType === FieldMetadataType.NUMBER) {
return parseInt(value);
}
if (fieldType === FieldMetadataType.BOOLEAN) {
return value.toLowerCase() === 'true';
switch (fieldType) {
case FieldMetadataType.NUMERIC:
return parseInt(value);
case FieldMetadataType.NUMBER:
case FieldMetadataType.POSITION:
return parseFloat(value);
case FieldMetadataType.BOOLEAN:
return value.toLowerCase() === 'true';
}
if (
(value[0] === '"' || value[0] === "'") &&
(value.charAt(value.length - 1) === '"' ||

View File

@ -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',
});
}

View File

@ -1,4 +1,28 @@
export const fetchMetadataFields = (objectNamePlural: string) => {
const fromRelations = `
toObjectMetadata {
id
dataSourceId
nameSingular
namePlural
isSystem
isRemote
}
toFieldMetadataId
`;
const toRelations = `
fromObjectMetadata {
id
dataSourceId
nameSingular
namePlural
isSystem
isRemote
}
fromFieldMetadataId
`;
const fields = `
type
name
@ -14,26 +38,12 @@ export const fetchMetadataFields = (objectNamePlural: string) => {
fromRelationMetadata {
id
relationType
toObjectMetadata {
id
dataSourceId
nameSingular
namePlural
isSystem
}
toFieldMetadataId
${fromRelations}
}
toRelationMetadata {
id
relationType
fromObjectMetadata {
id
dataSourceId
nameSingular
namePlural
isSystem
}
fromFieldMetadataId
${toRelations}
}
defaultValue
options
@ -69,25 +79,10 @@ export const fetchMetadataFields = (objectNamePlural: string) => {
return fields;
case 'relations':
return `
id
relationType
fromObjectMetadata {
id
dataSourceId
nameSingular
namePlural
isSystem
}
fromObjectMetadataId
toObjectMetadata {
id
dataSourceId
nameSingular
namePlural
isSystem
}
toObjectMetadataId
fromFieldMetadataId
toFieldMetadataId
${fromRelations}
${toRelations}
`;
}
};