Troubleshooting

Here are the scenarios you might come across where you may need to troubleshoot:

  1. Coming across an error during compilation: you cannot find any HTTP request header here.

    You get this error even after you have defined the Action using a RequestHeader.

    Most of the methods used in Play that deal with requests, expect an implicit RequestHeader. This convention has been followed in order to keep the code simple. For example, let's look at the controller trait here:

    trait Controller extends Results with BodyParsers with HttpProtocol with Status with HeaderNames with ContentTypes with RequestExtractors with Rendering { 
    
      //Provides an empty `Action` implementation: the result is a standard 'Not implemented yet' result page. 
      val TODO = Action { 
         NotImplemented[play.api.templates.Html](views.html.defaultpages.todo()) 
      } 
    
      //Retrieves the session implicitly from the request. 
      implicit def session(implicit request: RequestHeader) = request.session 
    
      //Retrieve the flash scope implicitly from the request. 
      implicit def flash(implicit request: RequestHeader) = request.flash 
    
      implicit def lang(implicit request: RequestHeader) = { 
        play.api.Play.maybeApplication.map { implicit app => 
          val maybeLangFromCookie = request.cookies.get(Play.langCookieName).flatMap(c => Lang.get(c.value)) 
            maybeLangFromCookie.getOrElse(play.api.i18n.Lang.preferred(request.acceptLanguages)) 
        }.getOrElse(request.acceptLanguages.headOption.getOrElse(play.api.i18n.Lang.defaultLang)) 
      } 
    }

    Notice that the session, flash, and lang methods accept an implicit parameter, such as a request, which is RequestHeader. It is in such cases that we need to mark the request header in our Action definition as implicit. Generally, it's safer to mark all the request headers as implicit in a Play application. So, to fix this error, we would need to modify our Action definition as follows:

    def foo = Action {
        implicit request => 
        … 
    }
  2. The request body for my GET request is not parsed. You may wonder why. The GET request is not expected to have a request body. Though the HTTP specification is not clear on this, in general practice, browsers do not forward the request body. Play body parser checks to see if the request is allowed to have a request body, that is, if the request is not a GET request, before parsing it.

    It is better to avoid a request body in your GET and DELETE requests. If you need to add a request body to these requests, maybe you should redesign the REST API for your application.

  3. You're not able to use the Play filters: GzipFilter, SecurityHeadersFilter, or CSRFFilter. You get an error: the object filters is not a member of package play, in line import play.filters.

    Filters is a separate module and needs to be included explicitly. You should add it the build.sbt file as the libraryDependencies += filters, and then reload the project.

  4. Coming across a compilation error when using Future: if you cannot find an implicit ExecutionContext, either require one for yourself or import ExecutionContext.Implicits.global. Why should this be done, though?

    Future requires an ExecutionContext, which defines the thread pool where threads will be allotted for an operation. Hence, you might get a compilation error when no ExecutionContext is available for Future. Refer to the Scala docs Futures ( http://docs.scala-lang.org/overviews/core/futures.html ) section for more on this.

  5. Coming across a runtime error while using the JSON parser: JsResultException:
    JsResultException(errors:List((,List(ValidationError(error.expected.jsstring,WrappedArray())))))] 

    This generally happens when the field being extracted from JSON is not present in the request body. This could be because there is a typo, for example, instead of emailId, and you might be sending an e-mail. You could use the asOpt method instead of as. For example:

    val emailId = (body"emailId"). asOpt[String]

    Then you could throw an error with a human-friendly message if that or any field is missing. Alternatively, you could pass default values using getOrElse.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.27.178