Skip to content

Commit

Permalink
add try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmd-azeez committed Dec 23, 2024
1 parent c170589 commit bc46b75
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
40 changes: 26 additions & 14 deletions servlets/json-schema/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,39 @@ function validate(input: CallToolRequest): CallToolResult {
throw new Error("Missing required argument: document");
}

const ajv = new Ajv();
const validate = ajv.compile(schema);
const valid = validate(document);
try {
const ajv = new Ajv();
const validate = ajv.compile(schema);
const valid = validate(document);

if (!valid) {
return {
content: [
{
type: ContentType.Text,
mimeType: "application/json",
text: JSON.stringify({ valid: false, errors: validate.errors}, null, 2)
}
]
if (!valid) {
return {
content: [
{
type: ContentType.Text,
mimeType: "application/json",
text: JSON.stringify({ valid: false, errors: validate.errors }, null, 2)
}
]
}
} else {
return {
content: [
{
type: ContentType.Text,
mimeType: "application/json",
text: JSON.stringify({ valid: true }, null, 2)
}
]
}
}
} else {
} catch (e) {
return {
content: [
{
type: ContentType.Text,
mimeType: "application/json",
text: JSON.stringify({ valid: true }, null, 2)
text: JSON.stringify({ valid: false, errors: [{ message: (e as any).message }] }, null, 2)
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion servlets/json-schema/test.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
xtp plugin call ./dist/plugin.wasm --wasi --input '{ "params": { "name": "validate", "arguments": { "schema": {"$schema":"http://json-schema.org/draft-07/schema#","title":"Person","type":"object","properties":{"name":{"type":"string","description":"The person'\''s full name"},"age":{"type":"integer","minimum":0,"description":"The person'\''s age in years"},"isStudent":{"type":"boolean","description":"Whether the person is a student"}},"required":["name","age"]}, "document": {"name":"John Doe","age":25,"isStudent":true} } } }' call
xtp plugin call ./dist/plugin.wasm --wasi --input '{ "params": { "name": "validate", "arguments": { "schema": {"$schema":"http://json-schema.org/draft-07/schema#","title":"Person","type":"object","properties":{"name":{"type":"string","description":"The person'\''s full name"},"age":{"type":"integer","minimum":0,"description":"The person'\''s age in years"},"isStudent":{"type":"boolean","description":"Whether the person is a student"}},"required":["name","age"]}, "document": {"namae":"John Doe","age":25,"isStudent":true} } } }' call

0 comments on commit bc46b75

Please sign in to comment.