graphile / graphile.github.io

PostGraphile (PostGraphQL) and Graphile-Build website - contributions very welcome!
https://www.graphile.org/
26 stars 127 forks source link

Document the way to return error messages to the UI from the DB #18

Open chadfurman opened 7 years ago

chadfurman commented 7 years ago

This is a good reference point: https://github.com/postgraphql/postgraphql/issues/485#issuecomment-330160947

chadfurman commented 7 years ago

Specifically:

-- check phone number format
if phone_number !~ '^\+[0-9]{8,20}$' then
  RAISE EXCEPTION 'Your phone number must start with a +, contain only numbers, and be at least 8 digits long.'
    USING ERRCODE = '12345';
  return null;
end if;

yields

{
  "data": {
    "authenticate": null
  },
  "errors": [
    {
      "message": "Your phone number must start with a +, contain only numbers, and be at least 8 digits long.",
      "locations": [
        {
          "line": 9,
          "column": 3
        }
      ],
      "path": [
        "authenticate"
      ]
    }
  ]
}

as well as the extendedErrors feature: https://github.com/postgraphql/postgraphql/pull/513