View Javadoc
1   package org.argeo.cms.integration;
2   
3   import java.io.IOException;
4   import java.io.Writer;
5   import java.util.ArrayList;
6   import java.util.List;
7   
8   import javax.servlet.http.HttpServletResponse;
9   
10  import org.apache.commons.logging.Log;
11  import org.apache.commons.logging.LogFactory;
12  
13  import com.fasterxml.jackson.core.JsonGenerator;
14  import com.fasterxml.jackson.core.JsonProcessingException;
15  import com.fasterxml.jackson.databind.ObjectMapper;
16  
17  /** Serialisable wrapper of a {@link Throwable}. */
18  public class CmsExceptionsChain {
19  	public final static Log log = LogFactory.getLog(CmsExceptionsChain.class);
20  
21  	private List<SystemException> exceptions = new ArrayList<>();
22  
23  	public CmsExceptionsChain() {
24  		super();
25  	}
26  
27  	public CmsExceptionsChain(Throwable exception) {
28  		writeException(exception);
29  		if (log.isDebugEnabled())
30  			log.error("Exception chain", exception);
31  	}
32  
33  	public String toJsonString(ObjectMapper objectMapper) {
34  		try {
35  			return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
36  		} catch (JsonProcessingException e) {
37  			throw new IllegalStateException("Cannot write system exceptions " + toString(), e);
38  		}
39  	}
40  
41  	public void writeAsJson(ObjectMapper objectMapper, Writer writer) {
42  		try {
43  			JsonGenerator jg = objectMapper.writerWithDefaultPrettyPrinter().getFactory().createGenerator(writer);
44  			jg.writeObject(this);
45  		} catch (IOException e) {
46  			throw new IllegalStateException("Cannot write system exceptions " + toString(), e);
47  		}
48  	}
49  
50  	public void writeAsJson(ObjectMapper objectMapper, HttpServletResponse resp) {
51  		try {
52  			resp.setContentType("application/json");
53  			resp.setStatus(500);
54  			writeAsJson(objectMapper, resp.getWriter());
55  		} catch (IOException e) {
56  			throw new IllegalStateException("Cannot write system exceptions " + toString(), e);
57  		}
58  	}
59  
60  	/** recursive */
61  	protected void writeException(Throwable exception) {
62  		SystemException systemException = new SystemException(exception);
63  		exceptions.add(systemException);
64  		Throwable cause = exception.getCause();
65  		if (cause != null)
66  			writeException(cause);
67  	}
68  
69  	public List<SystemException> getExceptions() {
70  		return exceptions;
71  	}
72  
73  	public void setExceptions(List<SystemException> exceptions) {
74  		this.exceptions = exceptions;
75  	}
76  
77  	/** An exception in the chain. */
78  	public static class SystemException {
79  		private String type;
80  		private String message;
81  		private List<String> stackTrace;
82  
83  		public SystemException() {
84  		}
85  
86  		public SystemException(Throwable exception) {
87  			this.type = exception.getClass().getName();
88  			this.message = exception.getMessage();
89  			this.stackTrace = new ArrayList<>();
90  			StackTraceElement[] elems = exception.getStackTrace();
91  			for (int i = 0; i < elems.length; i++)
92  				stackTrace.add("at " + elems[i].toString());
93  		}
94  
95  		public String getType() {
96  			return type;
97  		}
98  
99  		public void setType(String type) {
100 			this.type = type;
101 		}
102 
103 		public String getMessage() {
104 			return message;
105 		}
106 
107 		public void setMessage(String message) {
108 			this.message = message;
109 		}
110 
111 		public List<String> getStackTrace() {
112 			return stackTrace;
113 		}
114 
115 		public void setStackTrace(List<String> stackTrace) {
116 			this.stackTrace = stackTrace;
117 		}
118 
119 		@Override
120 		public String toString() {
121 			return "System exception: " + type + ", " + message + ", " + stackTrace;
122 		}
123 
124 	}
125 
126 	@Override
127 	public String toString() {
128 		return exceptions.toString();
129 	}
130 
131 //	public static void main(String[] args) throws Exception {
132 //		try {
133 //			try {
134 //				try {
135 //					testDeeper();
136 //				} catch (Exception e) {
137 //					throw new Exception("Less deep exception", e);
138 //				}
139 //			} catch (Exception e) {
140 //				throw new RuntimeException("Top exception", e);
141 //			}
142 //		} catch (Exception e) {
143 //			CmsExceptionsChain vjeSystemErrors = new CmsExceptionsChain(e);
144 //			ObjectMapper objectMapper = new ObjectMapper();
145 //			System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(vjeSystemErrors));
146 //			System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(e));
147 //			e.printStackTrace();
148 //		}
149 //	}
150 //
151 //	static void testDeeper() throws Exception {
152 //		throw new IllegalStateException("Deep exception");
153 //	}
154 
155 }