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