Handle ajv exceptions without crash the app

Hi guys!

I was wondering if there is a way to catch exceptions to avoid app crashes. I’m using react native with custom renderes. Whe ajv detects an error in the schema, it throws an exception that makes the app crash. I’m not sure if I’m missing something when I implemented the layouts that handles the error or if there is a way to handle errors when creating the ajv object.

Hi @leo,

What kind of exceptions is AJV throwing for you? We set strict:false so usually, there should be no exceptions.

In any case, if you need to add a try/catch arround AJV invocations then you could hand over a custom AJV instance to JSON Forms. For example you can hand over a proxy which try/catches all invocations on AJV’s methods.

We are working on a specific case, when enums has repeated keys. The ajv validation throws an exception about enum most not have duplicated items, this makes the app crash. So we would like to catch the error and present some message. We use ajv only for default values btw.

In JSON Forms we should catch compile errors like this and add a fallback, however for now we don’t do that.

As a fallback you can implement a React error boundary which is good practice anyway. Then whenever JSON Forms crashes for any reason, you can render a fallback UI.

As a workaround for this problem you can also hand over validationMode="NoValidation" to the JsonForms component. AJV will then not be invoked. This especially makes sense if you don’t use AJV for validation anyway.

If you need validation then you need to clean up the JSON Schema to be valid. Duplicated enums are not allowed and AJV will error out.

1 Like