Skip to content

Commit

Permalink
bubble up cwds error codes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidangb committed May 23, 2024
1 parent 681c7f5 commit 315af4f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.broadinstitute.dsde.firecloud.service.{TSVFileSupport, TsvTypes}
import org.broadinstitute.dsde.firecloud.utils.TSVLoadFile
import org.broadinstitute.dsde.rawls.model._
import org.broadinstitute.dsde.workbench.model.google.{GcsBucketName, GcsObjectName}
import org.databiosphere.workspacedata.client.ApiException
import spray.json.DefaultJsonProtocol._

import java.nio.charset.StandardCharsets
Expand Down Expand Up @@ -262,12 +263,16 @@ class EntityService(rawlsDAO: RawlsDAO, cwdsDAO: CwdsDAO, googleServicesDAO: Goo

getWorkspaceId(workspaceNamespace, workspaceName, userInfo) map { workspaceId =>
importToCWDS(workspaceNamespace, workspaceName, workspaceId, userInfo, importRequest)
} recover {
case apiEx:ApiException => throw wrapCwdsException(apiEx)
}
}

def listJobs(workspaceNamespace: String, workspaceName: String, runningOnly: Boolean, userInfo: UserInfo): Future[List[CwdsListResponse]] = {
rawlsDAO.getWorkspace(workspaceNamespace, workspaceName)(userInfo) map { workspace =>
cwdsDAO.listJobsV1(workspace.workspace.workspaceId, runningOnly)(userInfo)
} recover {
case apiEx:ApiException => throw wrapCwdsException(apiEx)
}
}

Expand All @@ -276,7 +281,23 @@ class EntityService(rawlsDAO: RawlsDAO, cwdsDAO: CwdsDAO, googleServicesDAO: Goo
val cwdsResponse = cwdsDAO.getJobV1(workspace.workspace.workspaceId, jobId)(userInfo)
logger.info(s"Found job $jobId in cWDS")
cwdsResponse
} recover {
case apiEx:ApiException => throw wrapCwdsException(apiEx)
}
}

private def wrapCwdsException(apiEx:ApiException) = {
def extractMessage(responseBody: String) = {
// attempt to extract a human-readable message from the API response. The API response is itself a json object
import spray.json._
responseBody.parseJson.asJsObject.fields("message") match {
case jss:JsString => jss.value
case jsv:JsValue => jsv.prettyPrint
}
}
// if human-readable message extraction fails, just default to the ApiException message
val errMsg = Try(extractMessage(apiEx.getResponseBody)).toOption.getOrElse(apiEx.getMessage)
new FireCloudExceptionWithErrorReport(ErrorReport(apiEx.getCode, errMsg))
}

def getEntitiesWithType(workspaceNamespace: String, workspaceName: String, userInfo: UserInfo): Future[PerRequestMessage] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ class WorkspaceApiServiceSpec extends BaseServiceSpec with WorkspaceApiService w
(Post(pfbImportPath, PFBImportRequest("https://bad.request.avro"))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(InternalServerError) // TODO: bubble up 400 / BadRequest
status should equal(BadRequest)
responseAs[String] should include ("Bad request as reported by cwds")
}
}
Expand All @@ -1025,7 +1025,7 @@ class WorkspaceApiServiceSpec extends BaseServiceSpec with WorkspaceApiService w
(Post(pfbImportPath, PFBImportRequest("https://forbidden.avro"))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(InternalServerError) // TODO: bubble up 403 / Forbidden
status should equal(Forbidden)
responseAs[String] should include ("Missing Authorization: Bearer token in header")
}
}
Expand All @@ -1034,7 +1034,7 @@ class WorkspaceApiServiceSpec extends BaseServiceSpec with WorkspaceApiService w
(Post(pfbImportPath, PFBImportRequest("https://its.lawsuit.time.avro"))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(InternalServerError) // TODO: bubble up 451 / UnavailableForLegalReasons
status should equal(UnavailableForLegalReasons)
responseAs[String] should include ("cwds message")
}
}
Expand Down Expand Up @@ -1069,7 +1069,7 @@ class WorkspaceApiServiceSpec extends BaseServiceSpec with WorkspaceApiService w
(Post(importJobPath, AsyncImportRequest("https://bad.request.avro", filetype))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(InternalServerError) // TODO: bubble up 400 / BadRequest
status should equal(BadRequest)
responseAs[String] should include ("Bad request as reported by cwds")
}
}
Expand All @@ -1078,7 +1078,7 @@ class WorkspaceApiServiceSpec extends BaseServiceSpec with WorkspaceApiService w
(Post(importJobPath, AsyncImportRequest("https://forbidden.avro", filetype))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(InternalServerError) // TODO: bubble up 403 / Forbidden
status should equal(Forbidden)
responseAs[String] should include ("Missing Authorization: Bearer token in header")
}
}
Expand All @@ -1087,7 +1087,7 @@ class WorkspaceApiServiceSpec extends BaseServiceSpec with WorkspaceApiService w
(Post(importJobPath, AsyncImportRequest("https://its.lawsuit.time.avro", filetype))
~> dummyUserIdHeaders(dummyUserId)
~> sealRoute(workspaceRoutes)) ~> check {
status should equal(InternalServerError) // TODO: bubble up 451 / UnavailableForLegalReasons
status should equal(UnavailableForLegalReasons)
responseAs[String] should include ("cwds message")
}
}
Expand Down

0 comments on commit 315af4f

Please sign in to comment.