Skip to content

Commit

Permalink
ErrorFactories.invalidArgumentWasNotFound for handling invalid event ids
Browse files Browse the repository at this point in the history
  • Loading branch information
tudor-da committed Oct 20, 2021
1 parent e4ee5eb commit 2a796ba
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,26 @@ class ErrorFactories private (errorCodesVersionSwitcher: ErrorCodesVersionSwitch
.asGrpcError,
)

// This error builder covers cases where existing logic handling invalid arguments returned NOT_FOUND.
def invalidArgumentWasNotFound(definiteAnswer: Option[Boolean])(message: String)(implicit
contextualizedErrorLogger: ContextualizedErrorLogger
): StatusRuntimeException =
errorCodesVersionSwitcher.choose(
v1 = {
val statusBuilder = Status
.newBuilder()
.setCode(Code.NOT_FOUND.value())
.setMessage(message)
addDefiniteAnswerDetails(definiteAnswer, statusBuilder)
grpcError(statusBuilder.build())
},
// TODO error codes: This error group is confusing for this generic error as it can be dispatched
// from call-sites that do not involve command validation (e.g. ApiTransactionService).
v2 = LedgerApiErrors.CommandValidation.InvalidArgument
.Reject(message)
.asGrpcError,
)

/** @param fieldName An invalid field's name.
* @param message A status' message.
* @param definiteAnswer A flag that says whether it is a definite answer. Provided only in the context of command deduplication.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,25 @@ class ErrorFactoriesSpec extends AnyWordSpec with Matchers with TableDrivenPrope
}
}

"return an invalidArgument (with legacy error code as NOT_FOUND) error" in {
val testCases = Table(
("definite answer", "expected details"),
(None, Seq.empty),
(Some(false), Seq(definiteAnswers(false))),
)

forEvery(testCases) { (definiteAnswer, expectedDetails) =>
assertVersionedError(_.invalidArgumentWasNotFound(definiteAnswer)("my message"))(
v1_code = Code.NOT_FOUND,
v1_message = "my message",
v1_details = expectedDetails,
v2_code = Code.INVALID_ARGUMENT,
v2_message =
s"INVALID_ARGUMENT(8,$correlationId): The submitted command has invalid arguments: my message",
)
}
}

"should create an ApiException without the stack trace" in {
val status = Status.newBuilder().setCode(Code.INTERNAL.value()).build()
val exception = grpcError(status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private[apiserver] final class ApiTransactionService private (
}
.getOrElse {
Future.failed {
errorFactories.invalidArgument(None)(s"invalid eventId: ${request.eventId}")
errorFactories.invalidArgumentWasNotFound(None)(s"invalid eventId: ${request.eventId}")
}
}
.andThen(logger.logErrorsOnCall[GetTransactionResponse])
Expand Down Expand Up @@ -185,7 +185,7 @@ private[apiserver] final class ApiTransactionService private (
}
.getOrElse {
val msg = s"eventId: ${request.eventId}"
Future.failed(errorFactories.invalidArgument(None)(msg))
Future.failed(errorFactories.invalidArgumentWasNotFound(None)(msg))
}
.andThen(logger.logErrorsOnCall[GetFlatTransactionResponse])
}
Expand Down

0 comments on commit 2a796ba

Please sign in to comment.