55

I want to implement a good error handling in my app, I have forced this file for catching the error.

App\Services\PayUService

try {
  $this->buildXMLHeader; // Should be $this->buildXMLHeader();
} catch (Exception $e) {
        return $e;
}

App\Controller\ProductController

function secTransaction(){
  if ($e) {
    return view('products.error', compact('e'));
  }
}

And this is what I get.

enter image description here

I don't know why Laravel is not redirecting me to the view. Is the error forced right?

0

1 Answer 1

176

You are inside a namespace so you should use \Exception to specify the global namespace:

try {

  $this->buildXMLHeader();

} catch (\Exception $e) {

    return $e->getMessage();
}

In your code you've used catch (Exception $e) so Exception is being searched in/as:

App\Services\PayUService\Exception

Since there is no Exception class inside App\Services\PayUService so it's not being triggered. Alternatively, you can use a use statement at the top of your class like use Exception; and then you can use catch (Exception $e).

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! I've already done it. I've seen it as a comment in the official documentation.
Can you please share more related code and also please tell me about secTransaction, what or how this method is innvolved?
The Alpha, the problem was the namespace. Thanks about worrying.
When you use \Exception, then that class is being searched in where? I mean what is the path of global classes?
In Global namespace, from the root.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.