1 package org
.argeo
.cms
.integration
;
3 import java
.io
.IOException
;
6 import javax
.servlet
.http
.HttpServletResponse
;
8 import org
.argeo
.api
.cms
.CmsLog
;
9 import org
.argeo
.util
.ExceptionsChain
;
11 import com
.fasterxml
.jackson
.core
.JsonGenerator
;
12 import com
.fasterxml
.jackson
.core
.JsonProcessingException
;
13 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
15 /** Serialisable wrapper of a {@link Throwable}. */
16 public class CmsExceptionsChain
extends ExceptionsChain
{
17 public final static CmsLog log
= CmsLog
.getLog(CmsExceptionsChain
.class);
19 public CmsExceptionsChain() {
23 public CmsExceptionsChain(Throwable exception
) {
25 if (log
.isDebugEnabled())
26 log
.error("Exception chain", exception
);
29 public String
toJsonString(ObjectMapper objectMapper
) {
31 return objectMapper
.writerWithDefaultPrettyPrinter().writeValueAsString(this);
32 } catch (JsonProcessingException e
) {
33 throw new IllegalStateException("Cannot write system exceptions " + toString(), e
);
37 public void writeAsJson(ObjectMapper objectMapper
, Writer writer
) {
39 JsonGenerator jg
= objectMapper
.writerWithDefaultPrettyPrinter().getFactory().createGenerator(writer
);
41 } catch (IOException e
) {
42 throw new IllegalStateException("Cannot write system exceptions " + toString(), e
);
46 public void writeAsJson(ObjectMapper objectMapper
, HttpServletResponse resp
) {
48 resp
.setContentType("application/json");
50 writeAsJson(objectMapper
, resp
.getWriter());
51 } catch (IOException e
) {
52 throw new IllegalStateException("Cannot write system exceptions " + toString(), e
);
56 // public static void main(String[] args) throws Exception {
61 // } catch (Exception e) {
62 // throw new Exception("Less deep exception", e);
64 // } catch (Exception e) {
65 // throw new RuntimeException("Top exception", e);
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();
76 // static void testDeeper() throws Exception {
77 // throw new IllegalStateException("Deep exception");