-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORE-247: try to fix 'Non-standard status codes cannot be created' er…
…ror (#1528)
- Loading branch information
Showing
6 changed files
with
86 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/scala/org/broadinstitute/dsde/firecloud/utils/StatusCodeUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.broadinstitute.dsde.firecloud.utils | ||
|
||
import akka.http.scaladsl.model.{StatusCode, StatusCodes} | ||
|
||
import scala.util.Try | ||
|
||
trait StatusCodeUtils { | ||
|
||
/** | ||
* Safely translates an integer to a StatusCode. This method avoids the RuntimeException | ||
* thrown by StatusCode.int2StatusCode when supplied with an unknown code and returns | ||
* a default status code instead. | ||
* | ||
* @param intCode the integer value to translate | ||
* @param default the code to return if the integer value is unknown; | ||
* defaults to Internal Server Error | ||
* @return the final status code | ||
*/ | ||
def statusCodeFrom(intCode: Int, default: Option[StatusCode] = None): StatusCode = | ||
Try(StatusCode.int2StatusCode(intCode)).getOrElse( | ||
default.getOrElse( | ||
StatusCodes.custom(intCode, | ||
reason = s"unknown status $intCode", | ||
defaultMessage = s"unknown status $intCode", | ||
isSuccess = false, | ||
allowsEntity = true | ||
) | ||
) | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/test/scala/org/broadinstitute/dsde/firecloud/utils/StatusCodeUtilsSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.broadinstitute.dsde.firecloud.utils | ||
|
||
import akka.http.scaladsl.model.StatusCode | ||
import akka.http.scaladsl.model.StatusCodes._ | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import org.scalatest.matchers.should.Matchers.convertToAnyShouldWrapper | ||
|
||
class StatusCodeUtilsSpec extends AnyFlatSpec with StatusCodeUtils { | ||
|
||
behavior of "statusCodeFrom" | ||
|
||
val expectedCases: Map[Int, StatusCode] = Map( | ||
200 -> OK, | ||
404 -> NotFound, | ||
503 -> ServiceUnavailable | ||
) | ||
|
||
expectedCases.foreach { case (intCode, statusCode) => | ||
it should s"handle known code $intCode" in { | ||
statusCodeFrom(intCode) shouldBe statusCode | ||
} | ||
} | ||
|
||
val unknownCodes: List[Int] = List(-1, 0, 42, 222, 555) | ||
|
||
unknownCodes.foreach { intCode => | ||
it should s"create a custom status code $intCode for unknown values" in { | ||
val actual = statusCodeFrom(intCode) | ||
actual.intValue() shouldBe intCode | ||
actual.isSuccess() shouldBe false | ||
actual.defaultMessage() shouldBe s"unknown status $intCode" | ||
} | ||
} | ||
|
||
unknownCodes.foreach { intCode => | ||
it should s"default unknown code $intCode to the caller-supplied default" in { | ||
statusCodeFrom(intCode, Option(ImATeapot)) shouldBe ImATeapot | ||
} | ||
} | ||
|
||
} |