13

So I have an error message that gets thrown in one file

$error_message = "Error received for " . $service . ": " . $_r['status'] . "\n" . "Message received: " . $_r['errors'];
throw new My_Exception($error_message);

and in another file I have

try {
    //blah blah
} catch( My_Exception $e ) { 
    var_export($e->getMessage());
}

The problem, however, is that $_r['errors'] is an ARRAY and it get $e->getMessage() just prints it as "Array". How can I modify this code to access the array?

0

4 Answers 4

17

The problem is that You're trying to merge array with a string. It'll always end like this.

Maybe You should pass to exception an array, so You can later make use of it?

<?php
class myException extends Exception {

    private $params;

    public function setParams(array $params) {
        $this->params = $params;
    }

    public function getParams() {
        return $this->params;
    }
}

// later it can be used like this:
try {
    $exception = new myException('Error!');
    $exception->setParams(array('status' => 1, 'errors' => array());

    throw $exception;
}
catch (myException $e) {
    // ...
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

This is bad form as it breaks the basic interface of exceptions. If you need to pass some values - just add a method that collects and stores them.
+1 Perfect. Now that object is gaining, rather than losing, functionality.
10

To convert a complex data-structure like an array into a string (e.g. for error messages), you can make use of print_r­Docs and setting it's second parameter to TRUE:

... ": " . print_r($_r['status'], TRUE) . "\n" ...

Comments

2

we can using json format

   throw new Exception(json_encode(['type'=>'error','isExit'=>'true','title'=>'SystemConfigError']));

and in the catch

        catch (Exception $error)
    {
        var_dump(json_decode($error->getMessage(),JSON_OBJECT_AS_ARRAY));
    }

Comments

0

so your example code is kinda bad, but assuming

$_r['errors'] = array(
    'Message 1',
    'Message 2',
    'Message 3',
    'Message 4',
    'Message 5',
);

Then

$error_message = "Error received for " . $service . ": \n" . impolode("\n", $_r['errors']) . "\n" . "Message received: " . $_r['errors'];
throw new My_Exception($error_message);

The key is taking your array of error messages and concatenating them all together with newlines (or whatever)

But I kinda agree with the comment that you may be using the Exception framework wrong. Can you post what you are trying to do?

The general rule of thumb is that you throw an exception for each unique event. You dont collect a bunch of error messages and then throw them all at once.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.