From 156aa825027ac19abdf97ddc14940b140608fda4 Mon Sep 17 00:00:00 2001 From: ssaleem-ee Date: Wed, 30 Jun 2021 07:50:57 +0100 Subject: [PATCH] TAV-302: Fixed json de-serialization error reporting --- app/util/AddressLookupJsonErrorHandler.scala | 41 ++++++++++++++++++++ conf/application.conf | 2 +- 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 app/util/AddressLookupJsonErrorHandler.scala diff --git a/app/util/AddressLookupJsonErrorHandler.scala b/app/util/AddressLookupJsonErrorHandler.scala new file mode 100644 index 0000000..a1c9c7d --- /dev/null +++ b/app/util/AddressLookupJsonErrorHandler.scala @@ -0,0 +1,41 @@ +/* + * Copyright 2021 HM Revenue & Customs + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package util + +import play.api.Configuration +import play.api.http.Status.BAD_REQUEST +import play.api.mvc.Results.BadRequest +import play.api.mvc.{RequestHeader, Result} +import uk.gov.hmrc.play.audit.http.connector.AuditConnector +import uk.gov.hmrc.play.bootstrap.backend.http.JsonErrorHandler +import uk.gov.hmrc.play.bootstrap.config.HttpAuditEvent + +import javax.inject.Inject +import scala.concurrent.{ExecutionContext, Future} + +class AddressLookupJsonErrorHandler @Inject()(auditConnector: AuditConnector, + httpAuditEvent: HttpAuditEvent, configuration: Configuration)( + implicit ec: ExecutionContext) extends JsonErrorHandler(auditConnector, httpAuditEvent, configuration) { + + override def onClientError(request: RequestHeader, statusCode: Int, message: String): Future[Result] = { + (statusCode, message) match { + case (BAD_REQUEST, msg) if msg.startsWith("Json validation error") => + Future.successful(BadRequest("error=missing or badly-formed postcode parameter")) + case _ => super.onClientError(request, statusCode, message) + } + } +} diff --git a/conf/application.conf b/conf/application.conf index bbe1c74..e6843fa 100644 --- a/conf/application.conf +++ b/conf/application.conf @@ -39,7 +39,7 @@ play.http.router = app.Routes #play 2.5 requires an application secret play.crypto.secret: "H7dVw$PlJiD)^U,oa4TA1pa]pT:4ETLqbL&2P=n6T~p,A*}^.Y46@PQOV~9(B09Hc]t7-hsf~&@w=zH" -play.http.errorHandler = "uk.gov.hmrc.play.bootstrap.http.JsonErrorHandler" +play.http.errorHandler = "util.AddressLookupJsonErrorHandler" play.ws.timeout.request = 70000ms # 70 secs