Search in sources :

Example 1 with RaiseException

use of org.jruby.exceptions.RaiseException in project enumerable by hraberg.

the class JRubyTest method convertedRubyProcRaisesArgumentErrorWhenCalledWithTooFewArguments.

@Test(expected = RaiseException.class)
public void convertedRubyProcRaisesArgumentErrorWhenCalledWithTooFewArguments() throws ScriptException {
    Ruby ruby = Ruby.getGlobalRuntime();
    try {
        RubyProc proc = toProc(Lambda.λ(s, s.toUpperCase()));
        proc.call(ruby.getThreadService().getCurrentContext(), new IRubyObject[0]);
    } catch (RaiseException e) {
        assertEquals(ruby.getArgumentError(), e.getException().getType());
        throw e;
    }
}
Also used : RubyProc(org.jruby.RubyProc) RaiseException(org.jruby.exceptions.RaiseException) Ruby(org.jruby.Ruby) LambdaJRuby(org.enumerable.lambda.support.jruby.LambdaJRuby) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) ScalaTest(org.enumerable.lambda.support.scala.ScalaTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

Example 2 with RaiseException

use of org.jruby.exceptions.RaiseException in project nokogiri by sparklemotion.

the class XmlReader method setAndRaiseErrorsIfAny.

private IRubyObject setAndRaiseErrorsIfAny(final Ruby runtime, final RaiseException ex) throws RaiseException {
    final ReaderNode currentNode = currentNode();
    if (currentNode == null)
        return runtime.getNil();
    if (currentNode.isError()) {
        RubyArray errors = (RubyArray) getInstanceVariable("@errors");
        IRubyObject error = currentNode.toSyntaxError();
        errors.append(error);
        setInstanceVariable("@errors", errors);
        throw ex != null ? ex : new RaiseException((XmlSyntaxError) error);
    }
    if (ex != null)
        throw ex;
    return this;
}
Also used : RubyArray(org.jruby.RubyArray) RaiseException(org.jruby.exceptions.RaiseException) ReaderNode(nokogiri.internals.ReaderNode) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 3 with RaiseException

use of org.jruby.exceptions.RaiseException in project nokogiri by sparklemotion.

the class XmlSaxPushParser method native_write.

@JRubyMethod
public IRubyObject native_write(ThreadContext context, IRubyObject chunk, IRubyObject isLast) {
    try {
        initialize_task(context);
    } catch (IOException e) {
        throw context.runtime.newRuntimeError(e.getMessage());
    }
    final ByteArrayInputStream data = NokogiriHelpers.stringBytesToStream(chunk);
    if (data == null) {
        terminateTask(context);
        // Nokogiri::XML::SyntaxError
        throw new RaiseException(XmlSyntaxError.createXMLSyntaxError(context.runtime));
    }
    int errorCount0 = parserTask.getErrorCount();
    if (isLast.isTrue()) {
        try {
            parserTask.parser.getNokogiriHandler().endDocument();
        } catch (SAXException e) {
            throw context.runtime.newRuntimeError(e.getMessage());
        }
        terminateTask(context);
    } else {
        try {
            Future<Void> task = stream.addChunk(data);
            task.get();
        } catch (ClosedStreamException ex) {
        // this means the stream is closed, ignore this exception
        } catch (Exception e) {
            throw context.runtime.newRuntimeError(e.toString());
        }
    }
    if (!options.recover && parserTask.getErrorCount() > errorCount0) {
        terminateTask(context);
        throw new RaiseException(parserTask.getLastError(), true);
    }
    return this;
}
Also used : ClosedStreamException(nokogiri.internals.ClosedStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) RaiseException(org.jruby.exceptions.RaiseException) IOException(java.io.IOException) RaiseException(org.jruby.exceptions.RaiseException) RubyException(org.jruby.RubyException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) ClosedStreamException(nokogiri.internals.ClosedStreamException) SAXException(org.xml.sax.SAXException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 4 with RaiseException

use of org.jruby.exceptions.RaiseException in project nokogiri by sparklemotion.

the class HtmlSaxPushParser method native_write.

@JRubyMethod
public IRubyObject native_write(ThreadContext context, IRubyObject chunk, IRubyObject isLast) {
    try {
        initialize_task(context);
    } catch (IOException e) {
        throw context.getRuntime().newRuntimeError(e.getMessage());
    }
    final ByteArrayInputStream data = NokogiriHelpers.stringBytesToStream(chunk);
    if (data == null) {
        terminateTask(context);
        // Nokogiri::HTML::SyntaxError
        throw new RaiseException(XmlSyntaxError.createHTMLSyntaxError(context.runtime));
    }
    int errorCount0 = parserTask.getErrorCount();
    if (isLast.isTrue()) {
        IRubyObject document = invoke(context, this, "document");
        invoke(context, document, "end_document");
        terminateTask(context);
    } else {
        try {
            Future<Void> task = stream.addChunk(data);
            task.get();
        } catch (ClosedStreamException ex) {
        // this means the stream is closed, ignore this exception
        } catch (Exception e) {
            throw context.getRuntime().newRuntimeError(e.getMessage());
        }
    }
    if (!options.recover && parserTask.getErrorCount() > errorCount0) {
        terminateTask(context);
        throw new RaiseException(parserTask.getLastError(), true);
    }
    return this;
}
Also used : ClosedStreamException(nokogiri.internals.ClosedStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) RaiseException(org.jruby.exceptions.RaiseException) IOException(java.io.IOException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) RaiseException(org.jruby.exceptions.RaiseException) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) RubyException(org.jruby.RubyException) IOException(java.io.IOException) ClosedStreamException(nokogiri.internals.ClosedStreamException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 5 with RaiseException

use of org.jruby.exceptions.RaiseException in project gocd by gocd.

the class XmlDomParserContext method getDocumentWithErrorsOrRaiseException.

public XmlDocument getDocumentWithErrorsOrRaiseException(ThreadContext context, RubyClass klazz, Exception ex) {
    if (options.recover) {
        XmlDocument xmlDocument = getInterruptedOrNewXmlDocument(context, klazz);
        this.addErrorsIfNecessary(context, xmlDocument);
        XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::SyntaxError"));
        xmlSyntaxError.setException(ex);
        ((RubyArray) xmlDocument.getInstanceVariable("@errors")).append(xmlSyntaxError);
        return xmlDocument;
    } else {
        XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::SyntaxError"));
        xmlSyntaxError.setException(ex);
        throw new RaiseException(xmlSyntaxError);
    }
}
Also used : RubyArray(org.jruby.RubyArray) XmlSyntaxError(nokogiri.XmlSyntaxError) RaiseException(org.jruby.exceptions.RaiseException) XmlDocument(nokogiri.XmlDocument)

Aggregations

RaiseException (org.jruby.exceptions.RaiseException)16 JRubyMethod (org.jruby.anno.JRubyMethod)8 RubyArray (org.jruby.RubyArray)7 IRubyObject (org.jruby.runtime.builtin.IRubyObject)7 ByteArrayInputStream (java.io.ByteArrayInputStream)4 IOException (java.io.IOException)4 ClosedStreamException (nokogiri.internals.ClosedStreamException)4 RubyException (org.jruby.RubyException)4 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)2 DOMSource (javax.xml.transform.dom.DOMSource)2 XmlDocument (nokogiri.XmlDocument)2 XmlSyntaxError (nokogiri.XmlSyntaxError)2 NokogiriHelpers.nodeArrayToRubyArray (nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray)2 ClojureTest (org.enumerable.lambda.support.clojure.ClojureTest)2 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)2 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)2 LambdaJRuby (org.enumerable.lambda.support.jruby.LambdaJRuby)2 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)2 Ruby (org.jruby.Ruby)2 RubyProc (org.jruby.RubyProc)2