Hi there,
I’m having quite the time figuring out how to include error codes in the responses from my Realm functions. A simple string message with name “Error” doesn’t meet my requirements. Unfortunately, the realm function will return a different format based on how it is used.
Correctly…
When I catch an error from a promise, and rethrow:
await new Promise((res, rej) => {
setTimeout(() => {
rej(JSON.stringify({message: "An error", code: 42}))
}, 300)
}).catch(err => {
throw err
})
and catch it in the client (.js
in this case)
await user.functions.errorTest().catch(errResponse => {
console.log(errResponse)
})
the errResponse.error
property is properly stringified:
{"code":42,"message":"An error"}
I believe this is the correct behaviour for the realm functions, and is what I expect to happen.
Unfortunately
From another realm function, when I throw a stringified javascript object:
throw JSON.stringify({message: "an error message", code: 42})
the response is double-stringified JSON:
"{\"code\":42,\"message\":\"An error\"}"
And the only way I can think of working with it is to parse it twice. My client is in swift so this is introducing instability and try catch expressions - which doesn’t make sense.
The reason I can’t return like so:
throw {message: "an error message", code: 42}
is because it will be stringified by the system as EJSON which I cannot work with. Also, why should I have to? I think this is a bug.