package org.simantics.document.server.request; import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.io.UnsupportedEncodingException; import org.simantics.Logger; import org.simantics.db.layer0.exception.MissingVariableException; import org.simantics.db.layer0.scl.SCLDatabaseException; import org.simantics.document.server.DocumentException; import org.simantics.scl.compiler.module.repository.ImportFailure; import org.simantics.scl.compiler.module.repository.ImportFailureException; import org.simantics.scl.compiler.top.NotFoundException; public class NodeRequestUtils { public static String formatErrorMessage(String name, Throwable t) { while(t.getCause() != null) t = t.getCause(); if (t instanceof DocumentException) { return t.getMessage(); } else if (t instanceof MissingVariableException) { return "Evaluation of property '" + name + "' failed\n" + t.getMessage(); } else if (t instanceof SCLDatabaseException) { return t.getMessage(); } else if (t instanceof NotFoundException) { return t.getMessage(); } else if (t instanceof ImportFailureException) { ImportFailureException e = (ImportFailureException)t; StringBuilder sb = new StringBuilder(); sb.append("The following SCL modules failed to compile:\n"); for(ImportFailure f : e.failures) sb.append(" " + f.moduleName + "\n"); return sb.toString(); } else { Logger.defaultLogError(t); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(baos); t.printStackTrace(ps); try { return baos.toString("UTF-8"); } catch (UnsupportedEncodingException e) { return baos.toString(); } } } }