Skip to content

Commit c9d273b

Browse files
authored
MTDSA-32419: Return 504 GATEWAY_TIMEOUT for 499s and 504s from downstream (#205)
1 parent bbcf34a commit c9d273b

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

app/shared/models/errors/CommonMtdErrors.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ object InternalError extends MtdError("INTERNAL_SERVER_ERROR", "An internal serv
5858

5959
object BadRequestError extends MtdError("INVALID_REQUEST", "Invalid request", BAD_REQUEST)
6060

61+
object GatewayTimeoutError extends MtdError("GATEWAY_TIMEOUT", "The request has timed out", GATEWAY_TIMEOUT)
62+
6163
object BVRError extends MtdError("BUSINESS_ERROR", "Business validation error", BAD_REQUEST)
6264

6365
object InvalidHttpMethodError extends MtdError("INVALID_HTTP_METHOD", "Invalid HTTP method", METHOD_NOT_ALLOWED)

app/shared/utils/ErrorHandler.scala

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,15 @@ class ErrorHandler @Inject() (
9797
ex
9898
)
9999

100+
val NGINX_TIMEOUT = 499
101+
val timeoutStatusCodes: Set[Int] = Set(NGINX_TIMEOUT, GATEWAY_TIMEOUT)
102+
100103
val (errorCode, eventType) = ex match {
101-
case _: NotFoundException => (NotFoundError, "ResourceNotFound")
102-
case _: AuthorisationException => (ClientOrAgentNotAuthorisedError.withStatus401, "ClientError")
103-
case _: JsValidationException => (BadRequestError, "ServerValidationError")
104-
case e: HttpException => (BadRequestError, "ServerValidationError")
104+
case _: NotFoundException => (NotFoundError, "ResourceNotFound")
105+
case _: AuthorisationException => (ClientOrAgentNotAuthorisedError.withStatus401, "ClientError")
106+
case _: JsValidationException => (BadRequestError, "ServerValidationError")
107+
case e: HttpException => (BadRequestError, "ServerValidationError")
108+
case e: UpstreamErrorResponse if timeoutStatusCodes.contains(e.statusCode) => (GatewayTimeoutError, "ServerTimeoutError")
105109
case e: UpstreamErrorResponse if UpstreamErrorResponse.Upstream4xxResponse.unapply(e).isDefined =>
106110
(BadRequestError, "ServerValidationError")
107111
case e: UpstreamErrorResponse if UpstreamErrorResponse.Upstream5xxResponse.unapply(e).isDefined =>

test/shared/utils/ErrorHandlerSpec.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ class ErrorHandlerSpec extends UnitSpec with GuiceOneAppPerSuite {
156156
status(result) shouldBe INTERNAL_SERVER_ERROR
157157
contentAsJson(result) shouldBe InternalError.asJson
158158
}
159+
160+
"return GATEWAY_TIMEOUT with error body" when {
161+
Seq(499, GATEWAY_TIMEOUT).foreach { statusCode =>
162+
s"a $statusCode UpstreamErrorResponse is returned" in new Test {
163+
val errorResponse: UpstreamErrorResponse = UpstreamErrorResponse("request timeout", statusCode, statusCode, Map.empty)
164+
val result: Future[Result] = handler.onServerError(requestHeader, errorResponse)
165+
166+
status(result) shouldBe GATEWAY_TIMEOUT
167+
contentAsJson(result) shouldBe GatewayTimeoutError.asJson
168+
}
169+
}
170+
}
159171
}
160172
}
161173

0 commit comments

Comments
 (0)