Unable to throw httpResponseException - asp.net-mvc-3

When I try to throw the below forbidden ResponseException from my controller. An exception stating "Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details." is catched in the catch block of the controller method. Need help in resolving this
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));

Just change your controller implementation to re-throw if it's an HttpResponseException:
try
{
// action implementation
}
catch (Exception e)
{
if (e is HttpResponseException)
{
throw e;
}
// error handling logic
}
But the better answer is that #1 - you should avoid be catching all exceptions, that's bad practice. And #2 - you should use an exception filter instead to do your error handling and not catch exceptions yourself.

Related

How to fix Sonar Violation: Invoke methods only conditionally and Either Log or rethrow the exception

I have the below statements in my code which violates some Sonar Rules.
LOG.info("Fetched: {}", mapper.writeValueAsString(requests));
This one shows Invoke method(s) only conditionally for mapper.writeValueAsString(requests).
}catch (Exception e) {
LOG.error("Exception occurred while trying to fetch requests", e);
throw new GenericException(Error.FETCH_REQUEST_ERR_001.name(), e.getMessage(), Error.FETCH_REQUEST_ERR_001.value());
}
This one shows Either log this exception and handle it, or rethrow it with some contextual information. rule violation.
Any idea on how to resolve these. Appreciate any help.
I use IntelliJ and in a similar situation as yours SonarLint plugin accepted the following fix for me:
if(LOG.isInfoEnabled() && mapper != null){
LOG.info("Fetched: {}", mapper.writeValueAsString(requests)); // Your code
}

Laravel 5.5 Try / Catch is not working it's execute the exception handle

I am working with laravel 5.5 I have written a code with try and catch exception. But Try / catch is not manage exception handling. Exception execute on the Exception/handle.php
Here is code I am following
try {
App\Models\justDoIt::find(1);
} catch (\Exception $ex) {
dd($ex);
report($ex);
return false;
}
I would like to know why catch is not executed and error is display from the handle.php in report()
Here is the handle.php code
public function report(Exception $exception) {
echo "Handle";
dd($exception);
parent::report($exception);
}
Result
Handle
FatalThrowableError {#284 ▼
#message: "Class 'App\Http\Controllers\App\Models\justDoIt' not found"
#code: 0
#file: "D:\xampp7\htdocs\homeexpert_nik\app\Http\Controllers\HomeController.php"
#line: 21
#severity: E_ERROR
trace: {▶}
}
Result will show from the handle.php file.
Your code is throwing an error, not an exception. You're trying to use a class that doesn't exist and PHP is complaining about it by throwing a FatalThrowableError.
In PHP 5, this code would have resulted in a fatal error message being rendered in the browser, however in PHP 7 (Which is required for Laravel 5.5), PHP now throws errors just like they were exceptions. This allows applications to catch these errors, just like exceptions using try/catch blocks.
The reason the error is escaping your try/catch block is that an error is not an Exception, or a child of it. The object being thrown is an Error. Both the Exception and Error classes implement a common interface, Throwable.
Throwable
- Exception
- Error
Laravel's exception handler is written to catch both of these classes in order to display the error page you're seeing.
If you were to change your code to the following, you would find that the } catch (Throwable $e) { block should be executed:
try {
App\Models\justDoIt::find(1);
} catch (\Exception $ex) {
dd('Exception block', $ex);
} catch (\Throwable $ex) {
dd('Throwable block', $ex);
}
For more information on this, see here.
An added extra: If you're wondering what the issue is with your code, it's because you likely forgot to use your model class in your controller:
use App\Models\justDoIt;

How can i catch Container exceptions?

I want to be able to catch container binding exceptions but it doesn't seem to work.
If i have this code:
try {
$instance = app()->make('SomeNonExistingBinding');
} catch (Exception $e) {
// handle failure
}
But somehow the thrown Exception is being caught in the Illuminate\Routing\Pipeline::prepareDestination() method that AFAIK converts the exceptions in HTTP responses, instead of my try-catch block.
Can someone help me on this, please?
Thanks in advance.

How to suppress Indy IdHTTP error message box?

I have an IdHTTP component and when I get a HTTP error (for example 404) Indy shows a message box. I want to handle this "silent" and prevent Indy from showing this.
I have not found any parameter to turn this off. Any ideas?
Indy does not display message boxes. It throws exceptions. There are default exception handlers inside the VCL/FMX framework that will display a message box to the user if an exception is not caught in your code. So simply catch the exception in your code, eg:
try
{
IdHTTP1->Get(...);
}
catch (const Exception &)
{
// do something...
}
If you need finer control over the exception filtering, all Indy-specific exceptions are derived from EIdException, and there are many descendants (like EIdHTTPProtocolException), eg:
try
{
IdHTTP1->Get(...);
}
catch (const EIdHTTPProtocolException &)
{
// an HTTP error occured, do something...
// details about the HTTP error are in the exception object
}
catch (const EIdException &)
{
// a non-HTTP Indy error occured, do something else...
}
catch (const Exception &)
{
// some other error occured, do something else...
}

Can't catch UnauthorizedAccessException (complains about inaccessible constructor)

I'm trying to catch an UnauthorizedAccessException, but
catch(UnauthorizedAccessException uae)
{
return FALSE;
}
gives the compiler error "unable to throw or catch a managed object by value or reference" and "cannot be caught as the destructor and/or copy constructor are inaccessible". If I try to catch as a pointer to the exception or by reference, it complains this exception cannot be handled by such a level of indirection.
You should catch by CLI pointer:
catch (UnauthorizedAccessException^ uae)

Resources