]> gerrit.simantics Code Review - simantics/platform.git/blob
ed0c54c968b8043e8fc7383655f8b9cf268a6236
[simantics/platform.git] /
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 "Evaluation of property '" + name + "' failed\n" +
28                                         t.getMessage();
29                 } else if (t instanceof SCLDatabaseException) {
30                         return t.getMessage();
31                 } else if (t instanceof NotFoundException) {
32                         return t.getMessage();
33                 } else if (t instanceof ImportFailureException) {
34                         ImportFailureException e = (ImportFailureException)t;
35                         StringBuilder sb = new StringBuilder();
36                         sb.append("The following SCL modules failed to compile:\n");
37                         for(ImportFailure f : e.failures)
38                                 sb.append(" " + f.moduleName + "\n");
39                         return sb.toString();
40                 } else {
41                     LOGGER.error("Node request error:", t);
42                     
43                         ByteArrayOutputStream baos = new ByteArrayOutputStream();
44                         PrintStream ps = new PrintStream(baos);
45                         t.printStackTrace(ps);
46                         try {
47                                 return baos.toString("UTF-8");
48                         } catch (UnsupportedEncodingException e) {
49                                 return baos.toString();
50                         }
51                         
52                 }
53         }
54         
55 }