Apex Handlebars project

152 views
Skip to first unread message

Cosmic Interloper

unread,
Mar 15, 2015, 12:13:46 AM3/15/15
to ve...@googlegroups.com
Hello

I have had some trouble getting an Apex/Javascript project to load a Handlebars templating engine

this is what i have  (i do have a templates directory, which contains a basic hbs template) :


$cat src/main/resources/server.js 

console.log('startup');

var Router = require("vertx-apex-js/router");

var router = Router.router(vertx);

console.log('basics loaded');

var HandlebarsTemplateEngine = require("vertx-apex-js/handlebars_template_engine");
var TemplateHandler = require("vertx-apex-js/template_handler");

console.log('templating loaded');



var engine = HandlebarsTemplateEngine.create();
console.log('engine created');

var handler = TemplateHandler.create(engine);

console.log('templating created');

router.route().handler(handler);
console.log('handler set');

vertx.createHttpServer().requestHandler(router.accept).listen(8080);
console.log('server running');



But i get:



$ ./gradlew clean; ./gradlew shadowJar; java -jar build/libs/jstest-3.0.0-SNAPSHOT-fat.jar
:clean

BUILD SUCCESSFUL

Total time: 6.309 secs
:compileJava UP-TO-DATE
:processResources
:classes
:shadowJar

BUILD SUCCESSFUL

Total time: 8.014 secs
startup
basics loaded
templating loaded
engine created
templating created
Mar 14, 2015 9:10:06 PM io.vertx.core.Starter
SEVERE: Failed in deploying verticle
javax.script.ScriptException: TypeError: function invoked with invalid arguments in <eval>#74:17<eval>@0 at line number 271 at column number 2
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:586)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:570)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:525)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:521)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:192)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
at io.vertx.lang.js.JSVerticleFactory$JSVerticle.start(JSVerticleFactory.java:96)
at io.vertx.core.impl.DeploymentManager.lambda$doDeploy$88(DeploymentManager.java:424)
at io.vertx.core.impl.DeploymentManager$$Lambda$15/594195094.handle(Unknown Source)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$3(ContextImpl.java:260)
at io.vertx.core.impl.ContextImpl$$Lambda$7/870698190.run(Unknown Source)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:380)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
at java.lang.Thread.run(Thread.java:745)
Caused by: <eval>#74:17<eval>@0:271:2 TypeError: function invoked with invalid arguments
at jdk.nashorn.internal.objects.NativeError.initException(NativeError.java:138)
at jdk.nashorn.internal.objects.NativeTypeError.<init>(NativeTypeError.java:73)
at jdk.nashorn.internal.objects.NativeTypeError.<init>(NativeTypeError.java:77)
at jdk.nashorn.internal.objects.NativeTypeError.constructor(NativeTypeError.java:98)
at jdk.nashorn.internal.scripts.Script$\^eval\_#74\!17\^eval\_@0.L:1$L:270(.vertx/debug-js/vertx-js/util/utils.js:271)
at jdk.nashorn.internal.scripts.Script$\^eval\_#74\!17\^eval\_@0.L:1$L:31$L:152(.vertx/debug-js/vertx-apex-js/route.js:159)
at jdk.nashorn.internal.scripts.Script$\^eval\_#74\!17\^eval\_@0.L:1(src/main/resources/server.js:24)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:545)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
at jdk.nashorn.internal.scripts.Script$\^eval\_.L:21$_load(.vertx/debug-js/vertx-js/util/jvm-npm.js:99)
at jdk.nashorn.internal.scripts.Script$\^eval\_.L:21$doRequire(.vertx/debug-js/vertx-js/util/jvm-npm.js:139)
at jdk.nashorn.internal.scripts.Script$\^eval\_.L:21$RequireNoCache(.vertx/debug-js/vertx-js/util/jvm-npm.js:110)
at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:1)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:535)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:568)
... 13 more


This error is rather confusing, it seems to get to 
router.route().handler(handler);

then have a problem on that line



my build.gradle:
buildscript {
    repositories { jcenter() }
    dependencies {
        classpath 'com.github.jengelman.gradle.plugins:shadow:1.1.1'
    }
}

apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'

version = '3.0.0-SNAPSHOT'

if (!JavaVersion.current().java8Compatible) {
    throw new IllegalStateException('''A Haiku:
                                      |  This needs Java 8,
                                      |  You are using something else,
                                      |  Refresh. Try again.'''.stripMargin())
}

repositories {
    mavenCentral()
    maven {
    }
}

dependencies {
    compile 'io.vertx:vertx-core:3.0.0-SNAPSHOT'
    compile 'io.vertx:vertx-lang-js:3.0.0-SNAPSHOT'
    compile 'io.vertx:vertx-apex:3.0.0-SNAPSHOT'
    compile 'io.vertx:vertx-auth-service:3.0.0-SNAPSHOT'
    compile 'com.github.jknack:handlebars:2.0.0'
}

shadowJar {
    classifier = 'fat'
    manifest {
        attributes 'Main-Class': 'io.vertx.core.Starter'
        attributes 'Main-Verticle': 'js:server.js'

    }

    mergeServiceFiles {
        include 'META-INF/services/io.vertx.core.spi.VerticleFactory'
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.0'
}


any help would be appreciated, thank you

Alexander Lehmann

unread,
Mar 15, 2015, 5:09:42 AM3/15/15
to ve...@googlegroups.com
which java version are you using? Julien mentioned something about a js bug in the versions below 8-40, maybe that is the same.

Alexander Lehmann

unread,
Mar 15, 2015, 5:27:35 AM3/15/15
to ve...@googlegroups.com
Come to think of it, it would be much easier to analyze the issue if you would put up a complete project e..g on github, the stack traces are hard to interpret as it is.




On Sunday, March 15, 2015 at 5:13:46 AM UTC+1, Cosmic Interloper wrote:

Cosmic Interloper

unread,
Mar 15, 2015, 1:26:33 PM3/15/15
to ve...@googlegroups.com
Thanks for getting back to me

here is my distilled project:
https://github.com/cinterloper/ApexJSTemplates

I more/less got my example code from here



[grant@Desktop jstest]$ ./gradlew clean; ./gradlew shadowJar;  java -jar build/libs/jstest-0.1-fat.jar
:clean UP-TO-DATE

BUILD SUCCESSFUL

Total time: 5.554 secs
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources
:classes
:shadowJar

BUILD SUCCESSFUL

Total time: 8.672 secs
Mar 15, 2015 10:24:42 AM io.vertx.core.Starter
SEVERE: Failed in deploying verticle
javax.script.ScriptException: TypeError: function invoked with invalid arguments in <eval>#74:17<eval>@0 at line number 271 at column number 2
at jdk.nashorn.api.scripting.NashornScriptEngine.throwAsScriptException(NashornScriptEngine.java:586)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:570)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:525)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:521)
at jdk.nashorn.api.scripting.NashornScriptEngine.eval(NashornScriptEngine.java:192)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
at io.vertx.lang.js.JSVerticleFactory$JSVerticle.start(JSVerticleFactory.java:96)
at io.vertx.core.impl.DeploymentManager.lambda$doDeploy$88(DeploymentManager.java:424)
at io.vertx.core.impl.DeploymentManager$$Lambda$5/1658785779.handle(Unknown Source)
at io.vertx.core.impl.ContextImpl.lambda$wrapTask$3(ContextImpl.java:260)
at io.vertx.core.impl.ContextImpl$$Lambda$6/1280443567.run(Unknown Source)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:380)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
at java.lang.Thread.run(Thread.java:745)
Caused by: <eval>#74:17<eval>@0:271:2 TypeError: function invoked with invalid arguments
at jdk.nashorn.internal.objects.NativeError.initException(NativeError.java:138)
at jdk.nashorn.internal.objects.NativeTypeError.<init>(NativeTypeError.java:73)
at jdk.nashorn.internal.objects.NativeTypeError.<init>(NativeTypeError.java:77)
at jdk.nashorn.internal.objects.NativeTypeError.constructor(NativeTypeError.java:98)
at jdk.nashorn.internal.scripts.Script$\^eval\_#74\!17\^eval\_@0.L:1$L:270(.vertx/debug-js/vertx-js/util/utils.js:271)
at jdk.nashorn.internal.scripts.Script$\^eval\_#74\!17\^eval\_@0.L:1$L:31$L:152(.vertx/debug-js/vertx-apex-js/route.js:159)
at jdk.nashorn.internal.scripts.Script$\^eval\_#74\!17\^eval\_@0.L:1(src/main/resources/server.js:14)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:545)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
at jdk.nashorn.internal.scripts.Script$\^eval\_.L:21$_load(.vertx/debug-js/vertx-js/util/jvm-npm.js:99)
at jdk.nashorn.internal.scripts.Script$\^eval\_.L:21$doRequire(.vertx/debug-js/vertx-js/util/jvm-npm.js:139)
at jdk.nashorn.internal.scripts.Script$\^eval\_.L:21$RequireNoCache(.vertx/debug-js/vertx-js/util/jvm-npm.js:110)
at jdk.nashorn.internal.scripts.Script$\^eval\_.runScript(<eval>:1)
at jdk.nashorn.internal.runtime.ScriptFunctionData.invoke(ScriptFunctionData.java:535)
at jdk.nashorn.internal.runtime.ScriptFunction.invoke(ScriptFunction.java:209)
at jdk.nashorn.internal.runtime.ScriptRuntime.apply(ScriptRuntime.java:378)
at jdk.nashorn.api.scripting.NashornScriptEngine.evalImpl(NashornScriptEngine.java:568)
... 13 more


Cosmic Interloper

unread,
Mar 15, 2015, 1:27:06 PM3/15/15
to ve...@googlegroups.com
also

[grant@Desktop jstest]$ java -version
openjdk version "1.8.0_31"
OpenJDK Runtime Environment (build 1.8.0_31-b13)
OpenJDK 64-Bit Server VM (build 25.31-b07, mixed mode)
[grant@Desktop jstest]$ 

Alexander Lehmann

unread,
Mar 15, 2015, 6:51:35 PM3/15/15
to ve...@googlegroups.com
Looks like this is not working at all, the actual error is that the variable passed to route.handler is not the correct type (should be function), that causes the exception.

This doesn't work with the example for MVEL templates either.

Cosmic Interloper

unread,
Mar 15, 2015, 8:51:05 PM3/15/15
to ve...@googlegroups.com

Yeah I saw that to, should I open a bug?

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Julien Viet

unread,
Mar 16, 2015, 3:14:43 AM3/16/15
to ve...@googlegroups.com, Alexander Lehmann
there is a bug fix and PR for lang-js here : https://github.com/vert-x3/vertx-lang-js/pull/13


-- 
Julien Viet
www.julienviet.com
--

Tim Fox

unread,
Mar 16, 2015, 7:13:59 AM3/16/15
to ve...@googlegroups.com
On 15/03/15 04:13, Cosmic Interloper wrote:
> Hello
>
> I have had some trouble getting an Apex/Javascript project to load a
> Handlebars templating engine
>
> this is what i have (i do have a templates directory, which contains a
> basic hbs template) :
>
>
> $cat src/main/resources/server.js
>
> console.log('startup');
>
> var Router = require("vertx-apex-js/router");
>
> var router = Router.router(vertx);
>
> console.log('basics loaded');
>
> var HandlebarsTemplateEngine =
> require("vertx-apex-js/handlebars_template_engine");
> var TemplateHandler = require("vertx-apex-js/template_handler");
>
> console.log('templating loaded');
>
>
>
> var engine = HandlebarsTemplateEngine.create();
> console.log('engine created');
>
> var handler = TemplateHandler.create(engine);
>
> console.log('templating created');
>
> router.route().handler(handler);

Does

router.route().handler(handler.handle);

work?

Julien Viet

unread,
Mar 16, 2015, 10:12:37 AM3/16/15
to ve...@googlegroups.com, Tim Fox
I think that it works well too.

I modified the code generator to generate this pattern for JS in our docs and examples.

-- 
Julien Viet
www.julienviet.com

Cosmic Interloper

unread,
Mar 16, 2015, 2:02:30 PM3/16/15
to ve...@googlegroups.com
Using handler.handle worked for me, this runs now:


https://github.com/cinterloper/ApexJSTemplates/blob/master/src/main/resources/server.js

Seeing it this way in the documentation/examples would help people i'm sure, if this is the correct way to do it.

Julien Viet

unread,
Mar 16, 2015, 2:29:29 PM3/16/15
to ve...@googlegroups.com, Cosmic Interloper
Reply all
Reply to author
Forward
0 new messages