Git Product home page Git Product logo

Comments (4)

tomakakwark avatar tomakakwark commented on July 26, 2024 1

Hi,

Thanks for your reply.

i solved the problem like you explain and changed the cast on catch

} catch (MailjetException $e) {

by

} catch (\Throwable $e) {

from laravel-mailjet.

uavn avatar uavn commented on July 26, 2024

@bldcaveman
This is expectable behavior, just catch the exception:

$response = null;
$error = [];

try {
    $response = Mailjet::get(Resources::$Contact, ['id' => '[email protected]']);
} catch (MailjetException $e) {
    if (404 === $e->getCode()) {
        // If user not found
        $error = [
            'code' => $e->getCode(),
            'message' => $e->getErrorMessage(),
         ];
    } else {
        // Other errors
        throw $e;
    }
}

var_dump($response ? $response->getData() : $error);
array(2) {
  ["code"]=>
  int(404)
  ["message"]=>
  string(16) "Object not found"
}

from laravel-mailjet.

tomakakwark avatar tomakakwark commented on July 26, 2024

Hi @uavn, I am facing the exact same problem but catching the error like you, does not prevent the exception from being thrown.

Mailjet\LaravelMailjet\Exception\MailjetException
MailjetService:get() failed: Object not found

try {
     $contactdata = Mailjet::get(Resources::$Contact, ['id' => $order->customer->email] );
} catch (MailjetException $e) {
      if (404 === $e->getCode()) {
                // If user not found
                $error = [
                    'code' => $e->getCode(),
                    'message' => $e->getErrorMessage(),
                 ];
        } else {
                // Other errors
                throw $e;
        }
 }

Can you help me with that ?

i'm using laravel 8x and laravel-mailjet 3x

Thanks in advance

from laravel-mailjet.

uavn avatar uavn commented on July 26, 2024

@tomakakwark
Hey, most probably you have non-404 exception code, so you came into } else { section, where

throw $e;

re-throws an exception, can you try to print in catch block exception code?

try {
     $contactdata = Mailjet::get(Resources::$Contact, ['id' => $order->customer->email] );
} catch (MailjetException $e) {
      echo 'Exception Code: ' . $e->getCode() . ';;;;;'; /// this line
      if (404 === $e->getCode()) {
                // If user not found
                $error = [
                    'code' => $e->getCode(),
                    'message' => $e->getErrorMessage(),
                 ];
        } else {
                // Other errors
                throw $e;
        }
 }

if code is not 404, you can try to handle that code, same as I handle 404:

try {
     $contactdata = Mailjet::get(Resources::$Contact, ['id' => $order->customer->email] );
} catch (MailjetException $e) {
      if (404 === $e->getCode()) { // changed here 404 to code, that you got from previous code
                // If user not found
                $error = [
                    'code' => $e->getCode(),
                    'message' => $e->getErrorMessage(),
                 ];
        } else {
                // Other errors
                throw $e;
        }
 }

for example:

try {
     $contactdata = Mailjet::get(Resources::$Contact, ['id' => $order->customer->email] );
} catch (MailjetException $e) {
      if (405 === $e->getCode()) { // 405 as example
                // If user not found
                $error = [
                    'code' => $e->getCode(),
                    'message' => $e->getErrorMessage(),
                 ];
        } else {
                // Other errors
                throw $e;
        }
 }

or even you try to handle all exceptions:

try {
     $contactdata = Mailjet::get(Resources::$Contact, ['id' => $order->customer->email] );
} catch (MailjetException $e) {
        $error = [
            'code' => $e->getCode(),
            'message' => $e->getErrorMessage(),
         ];
 }

from laravel-mailjet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.