Error handling in Mongo Realm Custom Resolver functions and better error responses

What is the best practice for handling and returning errors to the client when using a custom resolver?

I have setup a custom resolver in Mongo Realm and set it to use a custom function.

Within the function if I throw an error it only appears to be able to control the error.message that is sent back to the client.

My simplest example:

exports = async function () {
  throw 'An error message'
}

results in a return message of:

{
  "data": { "customResolver": null },
  "errors": [{
    "message": "An error message",
    "locations": [{ "line": 1, "column": 12 }],
    "path": ["customFunction"]
  }]
}

Am I missing something - is there a realm error type we can throw with additional properties?

Is there any way to control the response sent to the user - for example sending a status code of 401 unauthorised based on checking within the function (I tried using the can evaluate but this still returns 200).

Is there any way to modify the error structure - maybe adding additional fields such as a custom error code I could lookup on the client?

Is there any way of taking more control of the standard error messages that are returned from the Real GrpaQL endpoint? - for example the standard E10001 error for duplicate key is unhelpful and requires significant parsing on the client)

1 Like