use of org.codehaus.groovy.control.MultipleCompilationErrorsException in project workflow-cps-plugin by jenkinsci.
the class CpsFlowDefinitionValidator method toCheckStatus.
public static List<CheckStatus> toCheckStatus(CompilationFailedException x) {
List<CheckStatus> errors = new ArrayList<>();
if (x instanceof MultipleCompilationErrorsException) {
for (Object o : ((MultipleCompilationErrorsException) x).getErrorCollector().getErrors()) {
if (o instanceof SyntaxErrorMessage) {
SyntaxException cause = ((SyntaxErrorMessage) o).getCause();
CheckStatus st = new CheckStatus(cause.getOriginalMessage(), FAIL_KEY);
st.setLine(cause.getLine());
st.setColumn(cause.getStartColumn());
errors.add(st);
}
}
}
if (errors.isEmpty()) {
// Fallback to simple message
CheckStatus st = new CheckStatus(x.getMessage(), FAIL_KEY);
st.setLine(1);
st.setColumn(0);
errors.add(st);
}
return errors;
}
use of org.codehaus.groovy.control.MultipleCompilationErrorsException in project hale by halestudio.
the class GroovyScriptPage method addGroovyErrorAnnotation.
/**
* Add an error annotation to the given annotation model based on an
* exception that occurred while compiling or executing the Groovy Script.
*
* @param annotationModel the annotation model
* @param document the current document
* @param script the executed script, or <code>null</code>
* @param exception the occurred exception
*/
public static void addGroovyErrorAnnotation(IAnnotationModel annotationModel, IDocument document, Script script, Exception exception) {
// handle multiple groovy compilation errors
if (exception instanceof MultipleCompilationErrorsException) {
ErrorCollector errors = ((MultipleCompilationErrorsException) exception).getErrorCollector();
for (int i = 0; i < errors.getErrorCount(); i++) {
SyntaxException ex = errors.getSyntaxError(i);
if (ex != null) {
addGroovyErrorAnnotation(annotationModel, document, script, ex);
}
}
return;
}
Annotation annotation = new Annotation(SimpleAnnotations.TYPE_ERROR, false, exception.getLocalizedMessage());
Position position = null;
// single syntax exception
if (exception instanceof SyntaxException) {
int line = ((SyntaxException) exception).getStartLine() - 1;
if (line >= 0) {
try {
position = new Position(document.getLineOffset(line));
} catch (BadLocationException e1) {
log.warn("Wrong error position in document", e1);
}
}
}
// try to determine position from stack trace of script execution
if (position == null && script != null) {
for (StackTraceElement ste : exception.getStackTrace()) {
if (ste.getClassName().startsWith(script.getClass().getName())) {
int line = ste.getLineNumber() - 1;
if (line >= 0) {
try {
position = new Position(document.getLineOffset(line));
break;
} catch (BadLocationException e1) {
log.warn("Wrong error position in document", e1);
}
}
}
}
}
// fallback
if (position == null) {
position = new Position(0);
}
annotationModel.addAnnotation(annotation, position);
}
use of org.codehaus.groovy.control.MultipleCompilationErrorsException in project ontrack by nemerosa.
the class ExpressionEngineImpl method resolve.
public String resolve(final String expression, Map<String, ?> parameters) {
SandboxTransformer sandboxTransformer = new SandboxTransformer();
SecureASTCustomizer secure = new SecureASTCustomizer();
secure.setClosuresAllowed(false);
secure.setMethodDefinitionAllowed(false);
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.addCompilationCustomizers(sandboxTransformer, secure);
Binding binding = new Binding(parameters);
GroovyShell shell = new GroovyShell(binding, compilerConfiguration);
// Sandbox registration (thread level)
GroovyValueFilter sandboxFilter = new GroovyValueFilter() {
@Override
public Object filter(Object o) {
if (o == null || o instanceof String || o instanceof GString || o.getClass().getName().equals("Script1")) {
return o;
} else if (o instanceof Class) {
throw new ExpressionCompilationException(expression, String.format("%n- %s class cannot be accessed.", ((Class) o).getName()));
} else {
throw new ExpressionCompilationException(expression, String.format("%n- %s class cannot be accessed.", o.getClass().getName()));
}
}
};
try {
sandboxFilter.register();
Object result = shell.evaluate(expression);
if (result == null) {
return null;
} else if (!(result instanceof String)) {
throw new ExpressionNotStringException(expression);
} else {
return (String) result;
}
} catch (MissingPropertyException e) {
throw new ExpressionCompilationException(expression, "No such property: " + e.getProperty());
} catch (MultipleCompilationErrorsException e) {
StringWriter s = new StringWriter();
PrintWriter p = new PrintWriter(s);
@SuppressWarnings("unchecked") List<Message> errors = e.getErrorCollector().getErrors();
errors.forEach((Message message) -> writeErrorMessage(p, message));
throw new ExpressionCompilationException(expression, s.toString());
} finally {
sandboxFilter.unregister();
}
}
use of org.codehaus.groovy.control.MultipleCompilationErrorsException in project workflow-cps-plugin by jenkinsci.
the class LoadStepExecution method start.
@Override
public boolean start() throws Exception {
CpsStepContext cps = (CpsStepContext) getContext();
CpsThread t = CpsThread.current();
CpsFlowExecution execution = t.getExecution();
String text = cwd.child(step.getPath()).readToString();
String clazz = execution.getNextScriptName(step.getPath());
String newText = ReplayAction.replace(execution, clazz);
if (newText != null) {
listener.getLogger().println("Replacing Groovy text with edited version");
text = newText;
}
Script script;
try {
script = execution.getShell().parse(text);
} catch (MultipleCompilationErrorsException e) {
// Convert to a serializable exception, see JENKINS-40109.
throw new CpsCompilationErrorsException(e);
}
// execute body as another thread that shares the same head as this thread
// as the body can pause.
cps.newBodyInvoker(t.getGroup().export(script), true).withDisplayName(step.getPath()).withCallback(BodyExecutionCallback.wrap(cps)).start();
return false;
}
Aggregations