hot to best return Hubspot errors with php / jquery

AnwarJutt98
Member

Hubspot is returning errors i.e. for an exiting contact as a string looking something like this (dump) when an exception occurs:

Client error: 'POST https://api.hubapi.com/contacts/v1/contact?hapikey=***' resulted in a '409 Conflict' response: {"message":"Contact already exists","identityProfile":{"vid":851,"identity":[{"value":"xxx.xxx@xxx.com"," (truncated...)

What is the best way to access the values of "message" and "vid" in PHP or jQuery/Javascript? code

here's the php (in a livewire component but could easily be a controller etc. as well) code that makes the call:

try {
    $contact = Hubspot::contacts()->create([
        [ 'property' => 'email', 'value' => $this->email ],
        [ 'property' => 'firstname',    'value' => $this->firstname ],
        [ 'property' => 'lastname',     'value' => $this->lastname ],
        [ 'property' => 'phone',        'value' => $this->phone ],
        [ 'property' => 'gender',       'value' => $this->gender],
    ]);

    session([ 'hubspot_vid' => $contact->vid ]);
    $this->dispatchBrowserEvent('debug', 'Contact Created on Hubspot with the ID of: ' . $contact->vid);

} catch (\Exception $e) {
    dump($e->getMessage());
}

and this is how I (for the moment) process the response on the client side:

<script>
    window.addEventListener('debug', event => {
        console.log('Debug Msg.: ' + event.detail);
    })
</script>

I would love to be able to do something like:

$response = $e->getMessage(); // still the whole string returned?!
$message  = $response->message (or $response['message']) // returns: "Contact already exists" only
$id       = $response->vid (or $response['vid']) // returns: "851" only 
.
.

you'll get the idea.

0 Upvotes
1 Reply 1
TiphaineCuisset
Community Manager

Hi @AnwarJutt98 

 

Thank you for reaching out.

 

I want to tag some of our experts on this @himanshurauthan @tjoyce @zaklein do you have any thought for @AnwarJutt98 on this? 

 

Thank you!

Best

Tiphaine


Saviez vous que la Communauté est disponible en français?
Rejoignez les discussions francophones en changeant votre langue dans les paramètres !

Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !


0 Upvotes