Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(webv2): improve API validation error messages
joshistoast618d729f5526
diff --git a/invokeai/frontend/webv2/src/workbench/auth/schemas.test.ts b/invokeai/frontend/webv2/src/workbench/auth/schemas.test.tsindex fadd4c4248..59c5a1c87c 100644--- a/invokeai/frontend/webv2/src/workbench/auth/schemas.test.ts+++ b/invokeai/frontend/webv2/src/workbench/auth/schemas.test.ts@@ -116,7 +116,25 @@ describe('getApiErrorMessage', () => {it('unwraps the first validation issue', () => {const body = JSON.stringify({ detail: [{ loc: ['body', 'email'], msg: 'value is not a valid email address' }] });- expect(getApiErrorMessage(new ApiError(body, 422), 'fallback')).toBe('value is not a valid email address');+ expect(getApiErrorMessage(new ApiError(body, 422), 'fallback')).toBe('email: value is not a valid email address');+ });++ it('formats multiple-of validation issues with the field and received value', () => {+ const body = JSON.stringify({+ detail: [+ {+ ctx: { multiple_of: 16 },+ input: 888,+ loc: ['body', 'batch', 'graph', 'nodes', 'denoise_latents', 'flux2_denoise', 'height'],+ msg: 'Input should be a multiple of 16',+ type: 'multiple_of',+ },+ ],+ });++ expect(getApiErrorMessage(new ApiError(body, 422), 'fallback')).toBe(+ 'height must be a multiple of 16 (received 888).'+ );});it('falls back for non-JSON bodies and unknown errors', () => {diff --git a/invokeai/frontend/webv2/src/workbench/backend/http.ts b/invokeai/frontend/webv2/src/workbench/backend/http.tsindex 58a75737d2..399c644b56 100644--- a/invokeai/frontend/webv2/src/workbench/backend/http.ts+++ b/invokeai/frontend/webv2/src/workbench/backend/http.ts@@ -72,6 +72,49 @@ export class ApiError extends Error {}}+const humanizeFieldName = (value: string): string => value.replaceAll('_', ' ');++const getLastString = (values: unknown[]): string | null => {+ for (let index = values.length - 1; index >= 0; index -= 1) {+ const value = values[index];++ if (typeof value === 'string') {+ return value;+ }+ }++ return null;+};++const getValidationIssueMessage = (issue: unknown): string | null => {+ if (!issue || typeof issue !== 'object') {+ return null;+ }++ const record = issue as { ctx?: unknown; input?: unknown; loc?: unknown; msg?: unknown; type?: unknown };+ const loc = Array.isArray(record.loc) ? record.loc : [];+ const lastLoc = getLastString(loc);+ const field = lastLoc ? humanizeFieldName(lastLoc) : null;++ if (record.type === 'multiple_of') {+ const ctx = record.ctx && typeof record.ctx === 'object' ? (record.ctx as { multiple_of?: unknown }) : null;+ const multipleOf = ctx?.multiple_of;++ if (field && typeof multipleOf === 'number') {+ const received =+ typeof record.input === 'number' || typeof record.input === 'string' ? ` (received ${record.input})` : '';++ return `${field} must be a multiple of ${multipleOf}${received}.`;+ }+ }++ if (typeof record.msg === 'string' && record.msg) {+ return field ? `${field}: ${record.msg}` : record.msg;+ }++ return null;+};+export const assertOk = async (response: Response): Promise<Response> => {if (response.ok) {return response;@@ -119,10 +162,10 @@ export const getApiErrorMessage = (error: unknown, fallback: string): string =>// Validation errors come as a list of issues; surface the first one.if (Array.isArray(parsed.detail)) {- const first = parsed.detail[0] as { msg?: unknown } | undefined;+ const message = getValidationIssueMessage(parsed.detail[0]);- if (first && typeof first.msg === 'string') {- return first.msg;+ if (message) {+ return message;}}} catch {