1 package org
.argeo
.cms
.integration
;
3 import java
.io
.IOException
;
6 import javax
.servlet
.http
.HttpServletResponse
;
8 import org
.apache
.commons
.logging
.Log
;
9 import org
.apache
.commons
.logging
.LogFactory
;
10 import org
.argeo
.util
.ExceptionsChain
;
12 import com
.fasterxml
.jackson
.core
.JsonGenerator
;
13 import com
.fasterxml
.jackson
.core
.JsonProcessingException
;
14 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
16 /** Serialisable wrapper of a {@link Throwable}. */
17 public class CmsExceptionsChain
extends ExceptionsChain
{
18 public final static Log log
= LogFactory
.getLog(CmsExceptionsChain
.class);
20 public CmsExceptionsChain() {
24 public CmsExceptionsChain(Throwable exception
) {
26 if (log
.isDebugEnabled())
27 log
.error("Exception chain", exception
);
30 public String
toJsonString(ObjectMapper objectMapper
) {
32 return objectMapper
.writerWithDefaultPrettyPrinter().writeValueAsString(this);
33 } catch (JsonProcessingException e
) {
34 throw new IllegalStateException("Cannot write system exceptions " + toString(), e
);
38 public void writeAsJson(ObjectMapper objectMapper
, Writer writer
) {
40 JsonGenerator jg
= objectMapper
.writerWithDefaultPrettyPrinter().getFactory().createGenerator(writer
);
42 } catch (IOException e
) {
43 throw new IllegalStateException("Cannot write system exceptions " + toString(), e
);
47 public void writeAsJson(ObjectMapper objectMapper
, HttpServletResponse resp
) {
49 resp
.setContentType("application/json");
51 writeAsJson(objectMapper
, resp
.getWriter());
52 } catch (IOException e
) {
53 throw new IllegalStateException("Cannot write system exceptions " + toString(), e
);
57 // public static void main(String[] args) throws Exception {
62 // } catch (Exception e) {
63 // throw new Exception("Less deep exception", e);
65 // } catch (Exception e) {
66 // throw new RuntimeException("Top exception", e);
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();
76 // static void testDeeper() throws Exception {
77 // throw new IllegalStateException("Deep exception");