hot to best return Hubspot errors with php / jquery


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***' resulted in a '409 Conflict' response: {"message":"Contact already exists","identityProfile":{"vid":851,"identity":[{"value":""," (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) {

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

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

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.

1 Reply 1
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!



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 !