Regex to extract portion of raw error message (Stripe / stripe.js) [SOLVED]

Can you please help with the Regex syntax to extract the “message” section of the below raw stripe error. In this case, the message I am trying to extract is “Your card was declined.” - however, I would like to use a generic approach that will work on all similar Stripe error messages please.

Raw error from Stripe.js Plus - Update a customer's details: {
 "error": {
   "message": "Your card was declined.",
   "type": "card_error",
   "param": "",
   "code": "card_declined",
   "decline_code": "generic_decline"
 }
}

I am open to other ideas too on how to accomplish the same result without Regex. Essentially, I am trying to populate a generic alert with the correct stripped-down error message from Stripe. Thanks for the help!

If you’re using the Stripe.js plugin I am pretty sure you need to create an element event workflow:

and set an alert by pulling the error message from the token:

@philip - thanks. However, I am actually in the Charge portion of the workflow, rather than the Create Stripetoken portion of the workflow, so I don’t think that the This Stripetoken error provides the Stripe charge-related errors.

@CoBubble - any suggestions please?

Also, for everyone else, please send your Regex ideas as mentioned below (@NigelG @romanmg ?) Thanks!

Ah I see, didn’t realize that. While I play around with some ideas, check out http://regexr.com/ – a very helpful tool while learning regex.

Thanks @philip!

@mishav - perhaps you have some suggestions please?

Thanks all!

Try this regex:

(“message”):\s*([^,]*)/ig

…then further modify this by truncating the beginning 12 characters and truncating the 1 ending character. You may not need /ig depending on how Bubble parses this. I am sure there are improvements that can be made to this regex (in order to prevent having to further truncate), anyone is welcome to chime in :slight_smile:

Thanks @philip - I can confirm that it doesn’t work with /ig. When removing /ig it yelids:

"message": "Your card was declined."

So, we’re getting close. I’ll play around with it - thanks again!

Yes, that’s what I meant by needing truncate the beginning "message": " and ending ". You can do this within the Bubble editor. IMO the easiest route would be to save the resulting "message": "Your card was declined." as a custom state on your alert box, then on the alert box truncate this text you’re calling from the custom state itself.

It’s interesting, because after the Regex is added to the Bubble editor, the Truncate function vanishes…

Sorry just edited my last post, try this. (ideally this would all be done in regex as you requested, I’m still working on it :slight_smile: )

Thanks again @philip!

So, as the final solution, I used two find and replaces to remove the extra leading and trailing content:

Also, I found that this Regex worked:

"message": "([^"]+?)(?=").*

Thanks again @philip - I really appreciate the help.

3 Likes

I like the solution @philip and @collegefund.me! We’ll definitely look into improving error handling. :slight_smile:

1 Like