perwendel / spark

A simple expressive web framework for java. Spark has a kotlin DSL https://github.com/perwendel/spark-kotlin
Apache License 2.0
9.63k stars 1.56k forks source link

halt() methods force handlers to return Object #1025

Open tonvanbart opened 6 years ago

tonvanbart commented 6 years ago

Maybe more of a question; in the project I'm working on I noticed several handler methods that returned Object while actually they should return String (JSON payloads). On closer inspection this is because internally they were calling halt() to signal a server error or 404 Not Found in certain situations. Since the methods called halt instead of doing throw halt() the compiler forces a return statement, and since it's either returning a String or an exception (which is thrown but never returned anyway) the return type had to be Object. I've made a fork and changed the return to void, and the build still works with all tests passing. Is there any particular reason for the signature of the static halt() methods?

perwendel commented 5 years ago

the signature of the halt() methods are very old and the forcing of return is purely from limitations in Java. I'm not on a device with an IDE but wouldn't: `return halt(404, "not found"); work?

tonvanbart commented 5 years ago

Probably not, since that method still has a return type of HaltException where to me it seems it should be void (the halt() methods are one-liners which throw an exception, so they're guaranteed to never return anything). For clarification see this commit, after this the project build still completes without errors: https://github.com/tonvanbart/spark/commit/7218618d823882151acbcdd55898647c86642409

tonvanbart commented 5 years ago

@perwendel Hi, any update on this, or is there no time/priority?

ts14ic commented 4 years ago

I usually throw halt instead of return halt