1 /*
2 * Copyright (c) 2002-2014, Mairie de Paris
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice
10 * and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice
13 * and the following disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 *
16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * License 1.0
33 */
34 package fr.paris.lutece.portal.service.util;
35
36
37 /**
38 * This kind of exception is thrown when the application encounters a critical
39 * problem. This class extends RuntimeException in order to avoid try/catch
40 * blocks
41 */
42 public class AppException extends RuntimeException
43 {
44 /**
45 * Generated serialVersionUID
46 */
47 private static final long serialVersionUID = -742252097057629674L;
48 private Exception _exception; // Initial Exception to embed
49 private String _strMessage; // Error message
50
51 /**
52 * Constructor 1
53 *
54 * @param strMessage The error message
55 */
56 public AppException( String strMessage )
57 {
58 _strMessage = strMessage;
59 AppLogService.error( getPrintStack( this ) );
60 }
61
62 /**
63 * Constructor 2
64 *
65 * @param strMessage The error message
66 * @param e The initial exception
67 */
68 public AppException( String strMessage, Exception e )
69 {
70 _strMessage = strMessage;
71 _exception = e;
72 AppLogService.error( getAppMessage( ), e );
73 }
74
75 /**
76 * Constructor 3
77 */
78 public AppException( )
79 {
80 }
81
82 /**
83 * Returns the initial exception.
84 * @return The initial exception.
85 */
86 public Exception getInitialException( )
87 {
88 return _exception;
89 }
90
91 /**
92 * Overides getMessage method
93 * @return strMessage The error message
94 */
95 @Override
96 public String getMessage( )
97 {
98 return getAppMessage( );
99 }
100
101 /**
102 * Get the exception's printstack and returns it as a string
103 * @param e The Exception.
104 * @return The printstack as a String
105 */
106 private String getPrintStack( Exception e )
107 {
108 java.io.CharArrayWriter cw = new java.io.CharArrayWriter( );
109 java.io.PrintWriter pw = new java.io.PrintWriter( cw, true );
110 e.printStackTrace( pw );
111
112 return cw.toString( );
113 }
114
115 /**
116 * Overides getMessage method
117 * @return strMessage The error message
118 */
119 private String getAppMessage( )
120 {
121 StringBuffer strMessage = new StringBuffer( );
122 strMessage.append( _strMessage );
123 strMessage.append( "\n" );
124
125 /* Selects the initial exception, if it exists */
126 if ( getInitialException( ) != null )
127 {
128 strMessage.append( "Initial error print stack : \n" );
129 strMessage.append( getPrintStack( getInitialException( ) ) );
130 }
131
132 return strMessage.toString( );
133 }
134 }