Skip to content

Commit 77239f9

Browse files
committed
refactor: Improve error handling
1 parent dd9d0dc commit 77239f9

File tree

2 files changed

+68
-78
lines changed

2 files changed

+68
-78
lines changed

src/tools/run_manifest_validation/runValidation.ts

Lines changed: 66 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,68 +9,79 @@ import {getManifestSchema} from "../../utils/ui5Manifest.js";
99
const log = getLogger("tools:run_manifest_validation:runValidation");
1010
const schemaCache = new Map<string, Promise<object>>();
1111

12-
async function createUI5ManifestValidateFunction(ui5Schema: object) {
13-
const ajv = new Ajv2020.default({
14-
allErrors: true, // Collect all errors, not just the first one
15-
strict: false, // Allow additional properties that are not in schema
16-
unicodeRegExp: false,
17-
loadSchema: async (uri) => {
18-
// Check cache first to prevent infinite loops
19-
if (schemaCache.has(uri)) {
20-
log.info(`Loading cached schema: ${uri}`);
12+
// Configuration constants
13+
const AJV_SCHEMA_PATHS = {
14+
draft06: "node_modules/ajv/dist/refs/json-schema-draft-06.json",
15+
draft07: "node_modules/ajv/dist/refs/json-schema-draft-07.json",
16+
} as const;
2117

22-
try {
23-
const schema = await schemaCache.get(uri)!;
24-
return schema;
25-
} catch {
26-
schemaCache.delete(uri);
27-
}
28-
}
29-
30-
log.info(`Loading external schema: ${uri}`);
31-
let fetchSchema: Promise<object>;
32-
33-
try {
34-
if (uri.includes("adaptive-card.json")) {
35-
// Special handling for Adaptive Card schema to fix unsupported "id" property
36-
// According to the JSON Schema spec Draft 06 (used by Adaptive Card schema),
37-
// "$id" should be used instead of "id"
38-
fetchSchema = fetchCdn(uri)
39-
.then((response) => {
40-
if ("id" in response && typeof response.id === "string") {
41-
const typedResponse = response as Record<string, unknown>;
42-
typedResponse.$id = response.id;
43-
delete typedResponse.id;
44-
}
45-
return response;
46-
});
47-
} else {
48-
fetchSchema = fetchCdn(uri);
18+
async function createUI5ManifestValidateFunction(ui5Schema: object) {
19+
try {
20+
const ajv = new Ajv2020.default({
21+
allErrors: true, // Collect all errors, not just the first one
22+
strict: false, // Allow additional properties that are not in schema
23+
unicodeRegExp: false,
24+
loadSchema: async (uri) => {
25+
// Check cache first to prevent infinite loops
26+
if (schemaCache.has(uri)) {
27+
log.info(`Loading cached schema: ${uri}`);
28+
29+
try {
30+
const schema = await schemaCache.get(uri)!;
31+
return schema;
32+
} catch {
33+
schemaCache.delete(uri);
34+
}
4935
}
5036

51-
schemaCache.set(uri, fetchSchema);
52-
return fetchSchema;
53-
} catch (error) {
54-
log.warn(`Failed to load external schema ${uri}:` +
55-
`${error instanceof Error ? error.message : String(error)}`);
37+
log.info(`Loading external schema: ${uri}`);
38+
let fetchSchema: Promise<object>;
5639

57-
throw error;
58-
}
59-
},
60-
});
61-
const draft06MetaSchema = JSON.parse(
62-
await readFile("node_modules/ajv/dist/refs/json-schema-draft-06.json", "utf-8")
63-
) as AnySchemaObject;
64-
const draft07MetaSchema = JSON.parse(
65-
await readFile("node_modules/ajv/dist/refs/json-schema-draft-07.json", "utf-8")
66-
) as AnySchemaObject;
40+
try {
41+
if (uri.includes("adaptive-card.json")) {
42+
// Special handling for Adaptive Card schema to fix unsupported "id" property
43+
// According to the JSON Schema spec Draft 06 (used by Adaptive Card schema),
44+
// "$id" should be used instead of "id"
45+
fetchSchema = fetchCdn(uri)
46+
.then((response) => {
47+
if ("id" in response && typeof response.id === "string") {
48+
const typedResponse = response as Record<string, unknown>;
49+
typedResponse.$id = response.id;
50+
delete typedResponse.id;
51+
}
52+
return response;
53+
});
54+
} else {
55+
fetchSchema = fetchCdn(uri);
56+
}
57+
58+
schemaCache.set(uri, fetchSchema);
59+
return fetchSchema;
60+
} catch (error) {
61+
log.warn(`Failed to load external schema ${uri}:` +
62+
`${error instanceof Error ? error.message : String(error)}`);
63+
64+
throw error;
65+
}
66+
},
67+
});
68+
const draft06MetaSchema = JSON.parse(
69+
await readFile(AJV_SCHEMA_PATHS.draft06, "utf-8")
70+
) as AnySchemaObject;
71+
const draft07MetaSchema = JSON.parse(
72+
await readFile(AJV_SCHEMA_PATHS.draft07, "utf-8")
73+
) as AnySchemaObject;
6774

68-
ajv.addMetaSchema(draft06MetaSchema, "http://json-schema.org/draft-06/schema#");
69-
ajv.addMetaSchema(draft07MetaSchema, "http://json-schema.org/draft-07/schema#");
75+
ajv.addMetaSchema(draft06MetaSchema, "http://json-schema.org/draft-06/schema#");
76+
ajv.addMetaSchema(draft07MetaSchema, "http://json-schema.org/draft-07/schema#");
7077

71-
const validate = await ajv.compileAsync(ui5Schema);
78+
const validate = await ajv.compileAsync(ui5Schema);
7279

73-
return validate;
80+
return validate;
81+
} catch (error) {
82+
throw new Error(`Failed to create UI5 manifest validate function: ` +
83+
`${error instanceof Error ? error.message : String(error)}`);
84+
}
7485
}
7586

7687
async function readManifest(path: string) {

src/tools/run_manifest_validation/schema.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,6 @@ export const outputSchema = {
3535
.describe("The data validated by the keyword."),
3636
})
3737
).describe("Array of validation error objects as returned by Ajv."),
38-
39-
// errors: z.array(
40-
// z.object({
41-
// path: z.array(
42-
// z.any()
43-
// ).describe("An array of property keys or array offsets," +
44-
// "indicating where inside objects or arrays the instance was found"),
45-
// property: z.string()
46-
// .describe("Describes the property path. Starts with instance, and is delimited with a dot (.)"),
47-
// message: z.string()
48-
// .describe("A human-readable message for debugging use."),
49-
// instance: z.any()
50-
// .describe("The instance that failed"),
51-
// name: z.string()
52-
// .describe("The keyword within the schema that failed."),
53-
// argument: z.any()
54-
// .describe("Provides information about the keyword that failed."),
55-
// stack: z.string()
56-
// .describe("A human-readable string representing the error."),
57-
// }).describe("Single schema error object.")
58-
// ),
5938
};
60-
export const outputSchemaObject = z.object(outputSchema);
61-
export type RunSchemaValidationResult = z.infer<typeof outputSchemaObject>;
39+
const _outputSchemaObject = z.object(outputSchema);
40+
export type RunSchemaValidationResult = z.infer<typeof _outputSchemaObject>;

0 commit comments

Comments
 (0)