Search in sources :

Example 6 with MultipleCompilationErrorsException

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;
}
Also used : SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ArrayList(java.util.ArrayList) JSONObject(net.sf.json.JSONObject) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException)

Example 7 with MultipleCompilationErrorsException

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);
}
Also used : Position(org.eclipse.jface.text.Position) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) Annotation(org.eclipse.jface.text.source.Annotation) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 8 with MultipleCompilationErrorsException

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();
    }
}
Also used : Binding(groovy.lang.Binding) ExpressionNotStringException(net.nemerosa.ontrack.model.exceptions.ExpressionNotStringException) SecureASTCustomizer(org.codehaus.groovy.control.customizers.SecureASTCustomizer) Message(org.codehaus.groovy.control.messages.Message) ExceptionMessage(org.codehaus.groovy.control.messages.ExceptionMessage) MissingPropertyException(groovy.lang.MissingPropertyException) GString(groovy.lang.GString) GString(groovy.lang.GString) ExpressionCompilationException(net.nemerosa.ontrack.model.exceptions.ExpressionCompilationException) GroovyShell(groovy.lang.GroovyShell) SandboxTransformer(org.kohsuke.groovy.sandbox.SandboxTransformer) StringWriter(java.io.StringWriter) GroovyValueFilter(org.kohsuke.groovy.sandbox.GroovyValueFilter) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) List(java.util.List) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) PrintWriter(java.io.PrintWriter)

Example 9 with MultipleCompilationErrorsException

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;
}
Also used : CpsStepContext(org.jenkinsci.plugins.workflow.cps.CpsStepContext) Script(groovy.lang.Script) CpsCompilationErrorsException(org.jenkinsci.plugins.workflow.cps.CpsCompilationErrorsException) MultipleCompilationErrorsException(org.codehaus.groovy.control.MultipleCompilationErrorsException) CpsFlowExecution(org.jenkinsci.plugins.workflow.cps.CpsFlowExecution) CpsThread(org.jenkinsci.plugins.workflow.cps.CpsThread)

Aggregations

MultipleCompilationErrorsException (org.codehaus.groovy.control.MultipleCompilationErrorsException)9 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)5 GroovyShell (groovy.lang.GroovyShell)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)3 ErrorCollector (org.codehaus.groovy.control.ErrorCollector)3 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)3 Script (groovy.lang.Script)2 IOException (java.io.IOException)2 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)2 ExceptionMessage (org.codehaus.groovy.control.messages.ExceptionMessage)2 Message (org.codehaus.groovy.control.messages.Message)2 GradleException (org.gradle.api.GradleException)2 Binding (groovy.lang.Binding)1 GString (groovy.lang.GString)1 GroovyClassLoader (groovy.lang.GroovyClassLoader)1 GroovyCodeSource (groovy.lang.GroovyCodeSource)1 MissingPropertyException (groovy.lang.MissingPropertyException)1 File (java.io.File)1