mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
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:
parent
62c3687a9a
commit
c99e264bde
@ -56,3 +56,8 @@ export class FieldParseError extends Error {
|
|||||||
this.name = this.constructor.name;
|
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 {}
|
@ -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 { 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 {
|
import {
|
||||||
isArraySchemaObject,
|
isArraySchemaObject,
|
||||||
isInvocationFieldSchema,
|
isInvocationFieldSchema,
|
||||||
@ -42,7 +48,7 @@ const isCollectionFieldType = (fieldType: string) => {
|
|||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType => {
|
export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef | InvocationFieldSchema): FieldType => {
|
||||||
if (isInvocationFieldSchema(schemaObject)) {
|
if (isInvocationFieldSchema(schemaObject)) {
|
||||||
// Check if this field has an explicit type provided by the node schema
|
// Check if this field has an explicit type provided by the node schema
|
||||||
const { ui_type } = schemaObject;
|
const { ui_type } = schemaObject;
|
||||||
@ -72,7 +78,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
|
|||||||
// This is a single ref type
|
// This is a single ref type
|
||||||
const name = refObjectToSchemaName(allOf[0]);
|
const name = refObjectToSchemaName(allOf[0]);
|
||||||
if (!name) {
|
if (!name) {
|
||||||
throw new FieldParseError(t('nodes.unableToExtractSchemaNameFromRef'));
|
throw new UnableToExtractSchemaNameFromRefError(t('nodes.unableToExtractSchemaNameFromRef'));
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
name,
|
name,
|
||||||
@ -95,7 +101,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
|
|||||||
if (isRefObject(filteredAnyOf[0])) {
|
if (isRefObject(filteredAnyOf[0])) {
|
||||||
const name = refObjectToSchemaName(filteredAnyOf[0]);
|
const name = refObjectToSchemaName(filteredAnyOf[0]);
|
||||||
if (!name) {
|
if (!name) {
|
||||||
throw new FieldParseError(t('nodes.unableToExtractSchemaNameFromRef'));
|
throw new UnableToExtractSchemaNameFromRefError(t('nodes.unableToExtractSchemaNameFromRef'));
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
@ -118,7 +124,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
|
|||||||
|
|
||||||
if (filteredAnyOf.length !== 2) {
|
if (filteredAnyOf.length !== 2) {
|
||||||
// This is a union of more than 2 types, which we don't support
|
// This is a union of more than 2 types, which we don't support
|
||||||
throw new FieldParseError(
|
throw new UnsupportedUnionError(
|
||||||
t('nodes.unsupportedAnyOfLength', {
|
t('nodes.unsupportedAnyOfLength', {
|
||||||
count: filteredAnyOf.length,
|
count: filteredAnyOf.length,
|
||||||
})
|
})
|
||||||
@ -159,7 +165,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new FieldParseError(
|
throw new UnsupportedUnionError(
|
||||||
t('nodes.unsupportedMismatchedUnion', {
|
t('nodes.unsupportedMismatchedUnion', {
|
||||||
firstType,
|
firstType,
|
||||||
secondType,
|
secondType,
|
||||||
@ -178,7 +184,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
|
|||||||
if (isSchemaObject(schemaObject.items)) {
|
if (isSchemaObject(schemaObject.items)) {
|
||||||
const itemType = schemaObject.items.type;
|
const itemType = schemaObject.items.type;
|
||||||
if (!itemType || isArray(itemType)) {
|
if (!itemType || isArray(itemType)) {
|
||||||
throw new FieldParseError(
|
throw new UnsupportedArrayItemType(
|
||||||
t('nodes.unsupportedArrayItemType', {
|
t('nodes.unsupportedArrayItemType', {
|
||||||
type: itemType,
|
type: itemType,
|
||||||
})
|
})
|
||||||
@ -188,7 +194,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
|
|||||||
const name = OPENAPI_TO_FIELD_TYPE_MAP[itemType];
|
const name = OPENAPI_TO_FIELD_TYPE_MAP[itemType];
|
||||||
if (!name) {
|
if (!name) {
|
||||||
// it's 'null', 'object', or 'array' - skip
|
// it's 'null', 'object', or 'array' - skip
|
||||||
throw new FieldParseError(
|
throw new UnsupportedArrayItemType(
|
||||||
t('nodes.unsupportedArrayItemType', {
|
t('nodes.unsupportedArrayItemType', {
|
||||||
type: itemType,
|
type: itemType,
|
||||||
})
|
})
|
||||||
@ -204,7 +210,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
|
|||||||
// This is a ref object, extract the type name
|
// This is a ref object, extract the type name
|
||||||
const name = refObjectToSchemaName(schemaObject.items);
|
const name = refObjectToSchemaName(schemaObject.items);
|
||||||
if (!name) {
|
if (!name) {
|
||||||
throw new FieldParseError(t('nodes.unableToExtractSchemaNameFromRef'));
|
throw new UnableToExtractSchemaNameFromRefError(t('nodes.unableToExtractSchemaNameFromRef'));
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
name,
|
name,
|
||||||
@ -216,7 +222,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
|
|||||||
const name = OPENAPI_TO_FIELD_TYPE_MAP[schemaObject.type];
|
const name = OPENAPI_TO_FIELD_TYPE_MAP[schemaObject.type];
|
||||||
if (!name) {
|
if (!name) {
|
||||||
// it's 'null', 'object', or 'array' - skip
|
// it's 'null', 'object', or 'array' - skip
|
||||||
throw new FieldParseError(
|
throw new UnsupportedPrimitiveTypeError(
|
||||||
t('nodes.unsupportedArrayItemType', {
|
t('nodes.unsupportedArrayItemType', {
|
||||||
type: schemaObject.type,
|
type: schemaObject.type,
|
||||||
})
|
})
|
||||||
@ -232,7 +238,7 @@ export const parseFieldType = (schemaObject: OpenAPIV3_1SchemaOrRef): FieldType
|
|||||||
} else if (isRefObject(schemaObject)) {
|
} else if (isRefObject(schemaObject)) {
|
||||||
const name = refObjectToSchemaName(schemaObject);
|
const name = refObjectToSchemaName(schemaObject);
|
||||||
if (!name) {
|
if (!name) {
|
||||||
throw new FieldParseError(t('nodes.unableToExtractSchemaNameFromRef'));
|
throw new UnableToExtractSchemaNameFromRefError(t('nodes.unableToExtractSchemaNameFromRef'));
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
name,
|
name,
|
||||||
|
Loading…
Reference in New Issue
Block a user