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) {
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)^", " "));
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");
44 LOGGER.error("Node request error:", t);
46 ByteArrayOutputStream baos = new ByteArrayOutputStream();
47 PrintStream ps = new PrintStream(baos);
48 t.printStackTrace(ps);
50 return baos.toString("UTF-8");
51 } catch (UnsupportedEncodingException e) {
52 return baos.toString();