feat(ui): add more types of FieldParseError

Unfortunately you cannot test for both a specific type of error and match its message. Splitting the error classes makes it easier to test expected error conditions.
This commit is contained in:
psychedelicious 2024-02-13 22:53:30 +11:00 committed by Brandon Rising
parent 62c3687a9a
commit c99e264bde
2 changed files with 23 additions and 12 deletions

View File

@ -56,3 +56,8 @@ export class FieldParseError extends Error {
this.name = this.constructor.name;
}
}
export class UnableToExtractSchemaNameFromRefError extends FieldParseError {}
export class UnsupportedArrayItemType extends FieldParseError {}
export class UnsupportedUnionError extends FieldParseError {}
export class UnsupportedPrimitiveTypeError extends FieldParseError {}

View File

@ -1,6 +1,12 @@
import { FieldParseError } from 'features/nodes/types/error';
import {
FieldParseError,
UnableToExtractSchemaNameFromRefError,
UnsupportedArrayItemType,
UnsupportedPrimitiveTypeError,
UnsupportedUnionError,
} from 'features/nodes/types/error';
import type { FieldType } from 'features/nodes/types/field';
import type { OpenAPIV3_1SchemaOrRef } from 'features/nodes/types/openapi';
import type { InvocationFieldSchema, OpenAPIV3_1SchemaOrRef } from 'features/nodes/types/openapi';
import {
isArraySchemaObject,
isInvocationFieldSchema,
@ -42,7 +48,7 @@ const isCollectionFieldType = (fieldType: string) => {
return false;
};
export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType => {
export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef | InvocationFieldSchema): FieldType => {
if (isInvocationFieldSchema(schemaObject)) {
// Check if this field has an explicit type provided by the node schema
const { ui_type } = schemaObject;
@ -72,7 +78,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
// This is a single ref type
const name = refObjectToSchemaName(allOf[0]);
if (!name) {
throw new FieldParseError(t('nodes.unableToExtractSchemaNameFromRef'));
throw new UnableToExtractSchemaNameFromRefError(t('nodes.unableToExtractSchemaNameFromRef'));
}
return {
name,
@ -95,7 +101,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
if (isRefObject(filteredAnyOf[0])) {
const name = refObjectToSchemaName(filteredAnyOf[0]);
if (!name) {
throw new FieldParseError(t('nodes.unableToExtractSchemaNameFromRef'));
throw new UnableToExtractSchemaNameFromRefError(t('nodes.unableToExtractSchemaNameFromRef'));
}
return {
@ -118,7 +124,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
if (filteredAnyOf.length !== 2) {
// This is a union of more than 2 types, which we don't support
throw new FieldParseError(
throw new UnsupportedUnionError(
t('nodes.unsupportedAnyOfLength', {
count: filteredAnyOf.length,
})
@ -159,7 +165,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
};
}
throw new FieldParseError(
throw new UnsupportedUnionError(
t('nodes.unsupportedMismatchedUnion', {
firstType,
secondType,
@ -178,7 +184,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
if (isSchemaObject(schemaObject.items)) {
const itemType = schemaObject.items.type;
if (!itemType || isArray(itemType)) {
throw new FieldParseError(
throw new UnsupportedArrayItemType(
t('nodes.unsupportedArrayItemType', {
type: itemType,
})
@ -188,7 +194,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
const name = OPENAPI_TO_FIELD_TYPE_MAP[itemType];
if (!name) {
// it's 'null', 'object', or 'array' - skip
throw new FieldParseError(
throw new UnsupportedArrayItemType(
t('nodes.unsupportedArrayItemType', {
type: itemType,
})
@ -204,7 +210,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
// This is a ref object, extract the type name
const name = refObjectToSchemaName(schemaObject.items);
if (!name) {
throw new FieldParseError(t('nodes.unableToExtractSchemaNameFromRef'));
throw new UnableToExtractSchemaNameFromRefError(t('nodes.unableToExtractSchemaNameFromRef'));
}
return {
name,
@ -216,7 +222,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
const name = OPENAPI_TO_FIELD_TYPE_MAP[schemaObject.type];
if (!name) {
// it's 'null', 'object', or 'array' - skip
throw new FieldParseError(
throw new UnsupportedPrimitiveTypeError(
t('nodes.unsupportedArrayItemType', {
type: schemaObject.type,
})
@ -232,7 +238,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
} else if (isRefObject(schemaObject)) {
const name = refObjectToSchemaName(schemaObject);
if (!name) {
throw new FieldParseError(t('nodes.unableToExtractSchemaNameFromRef'));
throw new UnableToExtractSchemaNameFromRefError(t('nodes.unableToExtractSchemaNameFromRef'));
}
return {
name,