]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/integration/CmsExceptionsChain.java
Clean dependencies files.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / integration / CmsExceptionsChain.java
1 package org.argeo.cms.integration;
2
3 import java.io.IOException;
4 import java.io.Writer;
5
6 import javax.servlet.http.HttpServletResponse;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.argeo.util.ExceptionsChain;
11
12 import com.fasterxml.jackson.core.JsonGenerator;
13 import com.fasterxml.jackson.core.JsonProcessingException;
14 import com.fasterxml.jackson.databind.ObjectMapper;
15
16 /** Serialisable wrapper of a {@link Throwable}. */
17 public class CmsExceptionsChain extends ExceptionsChain {
18 public final static Log log = LogFactory.getLog(CmsExceptionsChain.class);
19
20 public CmsExceptionsChain() {
21 super();
22 }
23
24 public CmsExceptionsChain(Throwable exception) {
25 super(exception);
26 if (log.isDebugEnabled())
27 log.error("Exception chain", exception);
28 }
29
30 public String toJsonString(ObjectMapper objectMapper) {
31 try {
32 return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
33 } catch (JsonProcessingException e) {
34 throw new IllegalStateException("Cannot write system exceptions " + toString(), e);
35 }
36 }
37
38 public void writeAsJson(ObjectMapper objectMapper, Writer writer) {
39 try {
40 JsonGenerator jg = objectMapper.writerWithDefaultPrettyPrinter().getFactory().createGenerator(writer);
41 jg.writeObject(this);
42 } catch (IOException e) {
43 throw new IllegalStateException("Cannot write system exceptions " + toString(), e);
44 }
45 }
46
47 public void writeAsJson(ObjectMapper objectMapper, HttpServletResponse resp) {
48 try {
49 resp.setContentType("application/json");
50 resp.setStatus(500);
51 writeAsJson(objectMapper, resp.getWriter());
52 } catch (IOException e) {
53 throw new IllegalStateException("Cannot write system exceptions " + toString(), e);
54 }
55 }
56
57 // public static void main(String[] args) throws Exception {
58 // try {
59 // try {
60 // try {
61 // testDeeper();
62 // } catch (Exception e) {
63 // throw new Exception("Less deep exception", e);
64 // }
65 // } catch (Exception e) {
66 // throw new RuntimeException("Top exception", e);
67 // }
68 // } catch (Exception e) {
69 // CmsExceptionsChain vjeSystemErrors = new CmsExceptionsChain(e);
70 // ObjectMapper objectMapper = new ObjectMapper();
71 // System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(vjeSystemErrors));
72 // e.printStackTrace();
73 // }
74 // }
75 //
76 // static void testDeeper() throws Exception {
77 // throw new IllegalStateException("Deep exception");
78 // }
79
80 }