Provide Error Description When Error Occurs in Spring Boot - spring-boot

I m Trying to get Error Code And Error Description whenever Error is thrown From Try Block and as well as Error Class Name.
Whenever is error is thrown from try block i m getting Exception Class Name But I m not getting Exact Description why Error is occuring i need Description.
catch (Exception e) {
System.out.println("########################
e.getMessage()"+e.getMessage());
System.out.println("*******************************
e.getLocalizedMessage()"+e.getLocalizedMessage());
System.out.println("########################## e.getCause()
"+e.getCause());
System.out.println("************************** e.getClass()
"+e.getClass().getConstructors());
System.out.println("************************** e.getClass()
"+e.getClass().getName());
}
I want The Error Description why Error is Producing like if
DataIntegrityViolationException occurs i need on which constraint it just giving DataIntegrityViolationException error.

Related

Duplicate Key Exception from Mongo DB does not gets caught

Trying to insert duplicate documents in mongo db with help of mongo repository.
Snippet example:
try{ //insertion of duplicate doc in mongo db } catch(org.springframework.dao.DuplicateKeyException e){ // syso for something else for sake of this example }
I can see the exception getting thrown in console like errorMsg": "E11000 duplicate key error collection
But in code its not coming to catch block at all.
Why this exception cannot be caught in catch block?
Tried catching this exception with all the below exception instances:
org.springframework.dao.DuplicateKeyException,
com.mongodb.MongoWriteException,
Exception.
Since the exception is getting thrown and its evident from the logs it should be caught in catch block.

Guidewire Exception handling in UI

when internal Guidewire code throws an exception you get a nicely formatted error message box. However, when custom code throws an exception you are directed to the error page (with the red stack trace text & back to application button). Is there anything in the Guidewire framework to make proper UI handling of errors nicer?
such as: < TextBox value="user.someMethod()"/>
//someMethod code...
try{
return user.someOtherCode()
}catch(e : Exception){
//TODO: gracefully display erorr mesage on page
//e.g. showErrorMessage()
return null
}
You have a simpler way to do this,
The below piece of code can be written in helper class or any Enhancement or even in PCF code tab, this will return a nice formatted error message.
gw.api.util.LocationUtil.addRequestScopedErrorMessage("Your error message")
After some searching through Guidewire OOTB code UserDisplayableExceptions are what you need - answering myself in case someone else has this thought.
function goToPolicy(bulkDocumentDownload : BulkDocDownload_Avi) {
try {
throw new IndexOutOfBoundsException("Ops! some index out of bounds exception was thrown")
} catch (e : Exception) {
throw new com.guidewire.pl.web.controller.UserDisplayableException(e.Message)
}
}

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 to catch PSQLException value too long for type character varying

I'm currently testing the data access layer that I've created in spring (PersistenceContext is injected). So I have a stateless EJB that calls a service for example UserService, that inserts/delete/update data in the database.
The service works fine, I was able to insert database. But when I was testing and I input string value that is longer than the set length I got:
javax.transaction.RollbackException: Transaction marked for rollback.
WARNING: DTX5014: Caught exception in beforeCompletion() callback:
javax.persistence.PersistenceException: org.hibernate.exception.DataException: ERROR: value too long for type character varying(20)
Caused by: org.hibernate.exception.DataException: ERROR: value too long for type character varying(20)
Caused by: org.hibernate.exception.DataException: ERROR: value too long for type character varying(20)
My partial code:
#PersistenceContext
protected EntityManager entityManager;
try {
entityManager.persist(e);
} catch(Exception e) {
//log message here
}
Then I've tried everything to catch these errors but I was not able to. Any suggestion on how to resolve the issue?
Thanks,
czetsuya
I've used the following code to find out which error is thrown under your circumstances:
BEGIN;
CREATE TABLE t(v varchar(5));
DO $body$
BEGIN
INSERT INTO t VALUES ('1234567');
EXCEPTION WHEN OTHERS THEN
RAISE NOTICE '!!! %, %', SQLSTATE, SQLERRM;
END;$body$;
ROLLBACK;
You'll see, that error code is 22001, error is named string_data_right_truncation per PostrgeSQL's list of error codes.
I don't know how to catch this error in the Hibernate, but on the PL/pgSQL level you can do it using:
EXCEPTION WHEN SQLSTATE '22001' THEN
-- your code follows
END;
I hope this will help you.

Resources