graphql-engine: Hasura doesn't show RAISE EXCEPTION message from postgres function

It’s common to make some checks in triggers and RAISE NOTICE or EXCEPTION and provided message went to postgres log. Can we somehow provide this message to the client, instead poor Uncaught (in promise) Error: GraphQL error: postgres query error

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 37
  • Comments: 25 (7 by maintainers)

Commits related to this issue

Most upvoted comments

I guess you can use Class 22 — Data Exception error codes

raise exception like this RAISE EXCEPTION USING ERRCODE= ‘22000’, MESSAGE= ‘business logic error message’;

and the client will always receive { “errors”: [ { “extensions”: { “path”: “$”, “code”: “data-exception” }, “message”: “business logic error message” } ] }

https://www.postgresql.org/docs/current/errcodes-appendix.html

@0x777 that would be great. It would also be helpful if that check support a prefix or a pattern/mask to be configured, this could be in accordance to what is specified in the documentation.

When specifying an error code by SQLSTATE code, you are not limited to the predefined error codes, but can select any error code consisting of five digits and/or upper-case ASCII letters, other than 00000. It is recommended that you avoid throwing error codes that end in three zeroes, because these are category codes and can only be trapped by trapping the whole category.

In our use case today, we have a helper function (example below) that raises all exceptions that we want to propagate to the client. We use a prefix (eg: C0) to the code in order to indicate such application errors.

CREATE FUNCTION errors.raise(
  code_    TEXT DEFAULT 'C0500',
  message_ TEXT DEFAULT 'Something went wrong while processing your request',
  hint_    TEXT DEFAULT 'Try again later'
) RETURNS VOID
  LANGUAGE plpgsql
  IMMUTABLE STRICT PARALLEL SAFE AS
$function$
BEGIN
  RAISE EXCEPTION USING ERRCODE = code_, MESSAGE = message_, HINT = hint_;
END;
$function$;

It would really helpful if such a usage pattern (including the propagation of HINT) could be supported in the solution for this issue.

@abn This is definitely something that we want to implement but we haven’t added it to our pipeline yet. Hopefully, we can get this out in a couple of releases.

Any update on this feature? This would be great

This is a must feature.We really need this feature. When it will be available?

It raises proper exceptions if you use hasura-admin-secret but will fail if you use jwt authorization.

Any update? I think this is really needed for any kind of serious app with validations.

use this code it’s work

RAISE EXCEPTION USING ERRCODE = '22000', MESSAGE = 'Event is full';

for ERRCODE number you can only use in this list https://www.postgresql.org/docs/current/errcodes-appendix.html

Not sure this is needed anymore since you can set ERRCODE and that’ll cause the message to show. Awesome find, @sasog23!! Exactly what I was hoping for.

I’m not an expert here but I’m not sure NOTICE level messages could ever be passed through because of the way graphql clients expect responses. setting the errors field signifies an error for a graphql client, and would probably throw.

Maybe it would make sense to change the scope of this issue to target passing other exception info, like HINT and DETAILS.

It seems to be fixed in the last Hasura release https://github.com/hasura/graphql-engine/releases/tag/v1.2.0-beta.5 You have to put HASURA_GRAPHQL_DEV_MODE as true or start the server with --dev-mode flag.

I’m wondering why they consider this a dev setting while our usecase is pretty common ?

@abn This is definitely something that we want to implement but we haven’t added it to our pipeline yet. Hopefully, we can get this out in a couple of releases.