Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The message contents of Clerk:Errors is a serialized Ruby object as a string #42

Open
yoeun opened this issue Dec 8, 2023 · 0 comments

Comments

@yoeun
Copy link

yoeun commented Dec 8, 2023

I ran into this issue trying to match the error code form_identifier_exists in my Clerk::Errors::Fatal handler.

begin
  clerk_sdk.users.create(....)
rescue Clerk::Errors::Fatal => e
  e.errors                # NoMethodError for 'errors'
  e.message               # returns a what looks like Ruby hash object (but actually a string)
  e.message['error']      # NoMethodError for '[]'
end

I think the culprit is this line:

JSON.parse(response.body)

It takes the JSON response of the HTTP API and parses it into a ruby object. Then it passes this object into Errors::Fatal.new().

raise klass.new(body, status: response.status)

The end result is that the error object now has a message that looks like this:

{\"errors\"=>[{\"message\"=>\"That email address is taken. Please try another.\", \"long_message\"=>\"That email address is taken. Please try another.\", \"code\"=>\"form_identifier_exists\", \"meta\"=>{\"param_name\"=>\"email_address\"}}], \"clerk_trace_id\"=>\"...\", \"status\"=>422}

So it's not a ruby object or a JSON string, but a string representation of a ruby object.

There are ways to get around this by using eval() or replacing => with : and parsing as JSON, but I expected the JSON from the HTTP API to be mapped to properties in the Clerk::Errors::Fatal object.

rescue Clerk::Errors::Fatal => e
  # option 1: save JSON parsed HTTP error to `errors` property so I can inspect it myself
  e.errors[0].code

  # option 2: flatten HTTP response to single error
  e.code
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant