How to redirect to error page, If the URL is invalid? - scala

I am beginner to play scala framework. And I have created simple CRUD operation using it.
In my routes file I have declared all the necesary urls for different actions.
I want to know if user input some wrong url path from browser How should I display error page in play scala? Right now I am getting all the routes which are already defined.

I just found a solution for it
https://www.playframework.com/documentation/2.8.x/ScalaErrorHandling
this is the latest play scala documentation which handles such type of condition.
Since I am using runtime dependency injection (e.g. Guice), the error handler can be dynamically loaded at runtime.
The simplest way is to create a class in the root package called ErrorHandler that implements HttpErrorHandler.
If you place your error handler in the root package (i.e. package-less) and name it ErrorHandler, Play will use it by default.
But, in case you want to:
Add it inside a package;
Configure different error handlers for different environments;
Then add in application.conf the configuration property play.http.errorHandler pointing to your custom error handler class:
play.http.errorHandler = "com.example.ErrorHandler"

Related

Grails generate-controller makes a bad restful controller

I'm creating a restful api with grails 3.0.9 but if I generate the controller through the command line with: grails generate-controller [domain]
then my application will give an Grails runtime exception when I make a call to the api.
If I in the other hand create the controller manually (which extends the RestfulController) then there are no problems. I would like to get the controller generated so I can do some checks in save(), delete() and update()
This is the exception that I get:
ERROR org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[grailsDispatcherServlet] - Servlet.service() for servlet [grailsDispatcherServlet] in context with path [] threw exception [Could not resolve view with name 'index' in servlet with name 'grailsDispatcherServlet'] with root cause
javax.servlet.ServletException: Could not resolve view with name 'index' in servlet with name 'grailsDispatcherServlet'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1227) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1027) ~[spring-webmvc-4.1.7.RELEASE.jar:4.1.7.RELEASE]
It looks like it is trying to send the result to a view that does not exist, I should not need to create specific views for my rest api since it should only reply with the http status codes and some JSON.
Is this a bug or is the command generate-controller not designed for this?
I suspect you are generating a request that doesn't specify a format and doesn't specify an Accept header. If one of those is used to indicate that you want a JSON response, the default generated controller will respect that.
The project at https://github.com/jeffbrown/generatedcontroller demonstrates that the default generated controller with Grails 3.0.9 seems to work fine without any views.
What I an recommend is for you to install the templates and customize it so that it would produce a proper Restful controller.

Configure Swagger api with Play 2.4

Facing problem while configuring swagger api with play 2.4 framework.
Follow this url for configuration : https://github.com/swagger-api/swagger-play/tree/master/play-2.4/swagger-play2
After configuration gets a compile time error with message "type ApiHelpController is not a member of package controllers" as this ApiHelpController.scala file is present in app/controllers package.
Does anyone knows what i am missing.
Not sure what you are missing but let me show you an alternative for swagger play 2.4 integration
Unlike the one you were trying to use, this one does not require annotation, you write swagger spec directly in your routes files as comment. There are several benefits of this approach:
controller remain clean
you don't need to repeat path and parameters
you don't need to learn another API (the annotation api)
Also it generates swagger definition from case classes reflection.
Check it out:
https://github.com/iheartradio/play-swagger
Not sure about swagger-jaxrs, but swagger-play2 package works for me. You can refer to http://swagger.io/playing-with-swagger-using-swagger-and-swagger-ui-with-the-play-framework/

ATG browser components is not available

I want to create new companent in ATG. And if I forward to Browser Companent page, then I have 404 error. How I can fix this?
I assume that you have created a new component by creating a .properties file. The things to look for are
Is the file in an appropriate config layer? i.e. is it in a location on the configpath, such as the config directory of a module, or the localconfig directory of a server, where the application expects to look for .properties files?
Is the file a valid .properties file and properly formatted?
Is the class mentioned in the $class property correct, valid and fully qualified (package name and class name)
Has the named class been compiled successfully?
Is the named class available on the classpath? i.e. is it in the classes.jar of a module or in the classes directory (if that is how the module's class path has been defined?
Is the class a valid JavaBean - particularly, does it have a no-argument constructor?
Have you compiled, built and assembled your ATG application since creating this new component?
Have you restarted your ATG application since creating this new component?
Assuming this is all correct and yet you still cannot see your component, the next step is to troubleshoot why it is not visible.
Is the path to your component you are using in the dyn/admin component browser correct?
If the path is correct, and you still see an error in the browser, then take a look at the server logs (for around the time you first tried to access the component) - you might see an error or a sequence of errors telling you why the server could not instantiate the component
Can you go to the containing folder and see if the component is listed there - though it is OK if you cannot because it may not have been started if nothing is referencing it
Can you see your component in the ACC. Start the ACC, and go to View Components by Path and navigate to were you think your component should be. Can you see it there? If so, it means that your properties file is OK.
In the ACC, When you click on the puzzle-piece icon that represents your component, does it show it as being an instance of your class or as an instance of Object. If Object, it means that it cannot determine the class of the component
In the ACC, double-click the component and try to start it. Keep looking at the server logs as you do so
This should help you get to the bottom of things.

Sails js :: How to add custom validation error

Like Rails is there any way to add custom error message to validator?
Like:
if(this.password != this.passwordConfirmation){
this.errors.add('password', {rule: 'invalid'})
}
You can create custom validations on your models. Or create custom objects and inject them into your models to resusable code. Its actually in the docs!
http://sailsjs.org/#/documentation/concepts/ORM/Validations.html?q=custom-validation-rules
You can create a custom config file for error handling. You can reach that global config object by sails.config.error for example. Advantage of this solution is, that you can access this object in services and other places, where you have no access to the res object.
Next step would be creating a policy which would pass this config error object to res.locals. Or it could be handled in a response file, but I have no experience with that.
Out of the box, Sails.js does not support custom validation messages. But there's a workaround, using hooks.
http://sailsjs.org/documentation/concepts/models-and-orm/validations#?custom-validation-rules
Says the official site.

playframework scala - cannot find controllers.Application

As a test project to try out scala I've installed Play framework with the Scala module, following the instructions provided here:
http://scala.playframework.org/
The project gets created fine, but when I go to the home url I get a warning saying the server cannot find the default controller. I've not modified anything from the default install at this point, so my controller looks like this:
package controllers
import play._
import play.mvc._
object Application extends Controller {
def index = Template
}
Any ideas on what am I missing?
Controllers are called action generators. By default the template name will be resolved from the action method name. So if the controllers.Application.index method returns a Template, the app/views/Application/index.html template will be rendered.
Also try cleaning up and re-running the app, sometime it escapes the file updates.
Have a look here http://www.playframework.org/modules/scala-0.9/controllers