Skip to content

Commit 71747b7

Browse files
committed
LUTECE-1885 : prettier exception logging in standard FO/BO cases
1 parent 0695c3e commit 71747b7

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

src/java/fr/paris/lutece/portal/service/util/AppException.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
*/
3434
package fr.paris.lutece.portal.service.util;
3535

36+
import org.apache.commons.lang3.exception.ExceptionUtils;
37+
3638
/**
3739
* This kind of exception is thrown when the application encounters a critical problem. This class extends RuntimeException in order to avoid try/catch blocks
3840
* This class forces to write to the logs immediatly to protect against swallowing exceptions.
@@ -53,7 +55,7 @@ public class AppException extends RuntimeException
5355
public AppException( String strMessage )
5456
{
5557
super( strMessage );
56-
AppLogService.error( strMessage, this );
58+
writeToLogs( );
5759
}
5860

5961
/**
@@ -67,7 +69,7 @@ public AppException( String strMessage )
6769
public AppException( String strMessage, Throwable t )
6870
{
6971
super( strMessage, t );
70-
AppLogService.error( strMessage, this );
72+
writeToLogs( );
7173
}
7274

7375
/**
@@ -88,7 +90,7 @@ public AppException( String strMessage, Exception e )
8890
*/
8991
public AppException( )
9092
{
91-
AppLogService.error( this );
93+
writeToLogs( );
9294
}
9395

9496
/**
@@ -106,6 +108,30 @@ public AppException( )
106108
public AppException( String strMessage, Throwable t, boolean enableSuppression, boolean writableStackTrace )
107109
{
108110
super( strMessage, t, enableSuppression, writableStackTrace );
109-
AppLogService.error( strMessage, this );
111+
writeToLogs( );
112+
}
113+
114+
private void writeToLogs( )
115+
{
116+
StringBuilder sb = new StringBuilder( "Critical AppException" );
117+
118+
Throwable strRootCause = ExceptionUtils.getRootCause( this );
119+
if ( strRootCause != null )
120+
{
121+
sb.append ( ", root cause: " );
122+
String strShortName = strRootCause.getClass( ).getSimpleName( );
123+
sb.append( strShortName );
124+
}
125+
126+
Throwable throwableForMessage = strRootCause == null ? this : strRootCause;
127+
String strMessage = throwableForMessage.getMessage( );
128+
if ( strMessage != null )
129+
{
130+
sb.append ( ": " );
131+
sb.append( strMessage );
132+
}
133+
134+
String strHeader = sb.toString( );
135+
AppLogService.error( strHeader, this );
110136
}
111137
}

src/java/fr/paris/lutece/portal/web/PortalJspBean.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,16 @@ public String getError404Page( HttpServletRequest request )
340340
*/
341341
public String getError500Page( HttpServletRequest request, Throwable exception )
342342
{
343-
//AppException calls AppLogService.error in the constructor, so don't call it here again
344-
if ( ! ( exception instanceof AppException ) ) {
343+
if ( exception instanceof AppException )
344+
{
345+
//AppException calls AppLogService.error( message, this ) in the
346+
//constructor, so don't call it here again Call toString to have
347+
//the Class and the message to be able to indentify the correct
348+
//stacktrace in the preceding logs.
349+
AppLogService.error( "Error 500 : Caused by previous Critical AppException" );
350+
}
351+
else
352+
{
345353
AppLogService.error( "Error 500 : " + exception.getMessage( ), exception );
346354
}
347355

webapp/jsp/admin/ErrorPage.jsp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,18 @@
8787
<div class="alert alert-error">
8888
<h3>Internal error</h3>
8989
<%
90-
AppLogService.error( exception.getMessage( ), exception );
90+
if ( exception instanceof AppException )
91+
{
92+
//AppException calls AppLogService.error( message, this ) in the
93+
//constructor, so don't call it here again Call toString to have
94+
//the Class and the message to be able to indentify the correct
95+
//stacktrace in the preceding logs.
96+
AppLogService.error( "Error 500 : Caused by previous Critical AppException" );
97+
}
98+
else
99+
{
100+
AppLogService.error( exception.getMessage( ), exception );
101+
}
91102
if( AppPropertiesService.getProperty( PROPERTY_DEBUG , PROPERTY_DEBUG_DEFAULT ).equalsIgnoreCase( "true" ))
92103
{
93104
%>

0 commit comments

Comments
 (0)