1 package org.simantics.document.server.request;
3 import java.io.ByteArrayOutputStream;
4 import java.io.PrintStream;
5 import java.io.UnsupportedEncodingException;
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;
16 public class NodeRequestUtils {
17 private static final Logger LOGGER = LoggerFactory.getLogger(NodeRequestUtils.class);
19 public static String formatErrorMessage(String name, Throwable t) {
21 while(t.getCause() != null)
24 if (t instanceof DocumentException) {
25 return t.getMessage();
26 } else if (t instanceof MissingVariableException) {
27 return "Evaluation of property '" + name + "' failed\n" +
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");
41 LOGGER.error("Node request error:", t);
43 ByteArrayOutputStream baos = new ByteArrayOutputStream();
44 PrintStream ps = new PrintStream(baos);
45 t.printStackTrace(ps);
47 return baos.toString("UTF-8");
48 } catch (UnsupportedEncodingException e) {
49 return baos.toString();