]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.server/src/org/simantics/document/server/request/NodeRequestUtils.java
Do not override jersey in p2.site pom.xml as release/1.43.0 is jdk8
[simantics/platform.git] / bundles / org.simantics.document.server / src / org / simantics / document / server / request / NodeRequestUtils.java
1 package org.simantics.document.server.request;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.PrintStream;
5 import java.io.UnsupportedEncodingException;
6
7 import org.simantics.db.layer0.exception.MissingVariableException;
8 import org.simantics.db.layer0.scl.SCLDatabaseException;
9 import org.simantics.document.server.DocumentException;
10 import org.simantics.scl.compiler.module.repository.ImportFailure;
11 import org.simantics.scl.compiler.module.repository.ImportFailureException;
12 import org.simantics.scl.compiler.top.NotFoundException;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 public class NodeRequestUtils {
17     private static final Logger LOGGER = LoggerFactory.getLogger(NodeRequestUtils.class);
18
19         public static String formatErrorMessage(String name, Throwable t) {
20                 
21                 while(t.getCause() != null)
22             t = t.getCause();
23                 
24                 if (t instanceof DocumentException) {
25                         return t.getMessage();
26                 } else if (t instanceof MissingVariableException) {
27                         return name + ":\n" +
28                                         t.getMessage().replaceAll("(?m)^", "  ");
29                 } else if (t instanceof SCLDatabaseException) {
30                         StringBuilder sb = new StringBuilder();
31                         sb.append(name + ":\n");
32                         sb.append(t.getMessage().replaceAll("(?m)^", "  "));
33                         return sb.toString();
34                 } else if (t instanceof NotFoundException) {
35                         return t.getMessage();
36                 } else if (t instanceof ImportFailureException) {
37                         ImportFailureException e = (ImportFailureException)t;
38                         StringBuilder sb = new StringBuilder();
39                         sb.append("The following SCL modules failed to compile:\n");
40                         for(ImportFailure f : e.failures)
41                                 sb.append(" " + f.moduleName + "\n");
42                         return sb.toString();
43                 } else {
44                     LOGGER.error("Node request error:", t);
45                     
46                         ByteArrayOutputStream baos = new ByteArrayOutputStream();
47                         PrintStream ps = new PrintStream(baos);
48                         t.printStackTrace(ps);
49                         try {
50                                 return baos.toString("UTF-8");
51                         } catch (UnsupportedEncodingException e) {
52                                 return baos.toString();
53                         }
54                         
55                 }
56         }
57         
58 }