diff --git a/internal/internal_nexus_task_handler.go b/internal/internal_nexus_task_handler.go index c503332c1..618acf86f 100644 --- a/internal/internal_nexus_task_handler.go +++ b/internal/internal_nexus_task_handler.go @@ -233,8 +233,7 @@ func (h *nexusTaskHandler) handleStartOperation( }, }, nil, nil } - // Default to expose details for now. We may make this configurable eventually. - err = convertServiceError(convertApplicationError(err), true) + err = convertKnownErrors(err) var handlerErr *nexus.HandlerError if errors.As(err, &handlerErr) { return nil, nexusHandlerErrorToProto(handlerErr), nil @@ -306,8 +305,7 @@ func (h *nexusTaskHandler) handleCancelOperation(ctx context.Context, nctx *Nexu return nil, nil, ctx.Err() } if err != nil { - // Default to expose details for now. We may make this configurable eventually. - err = convertServiceError(convertApplicationError(err), true) + err = convertKnownErrors(err) var handlerErr *nexus.HandlerError if errors.As(err, &handlerErr) { return nil, nexusHandlerErrorToProto(handlerErr), nil @@ -423,9 +421,17 @@ func (p *payloadSerializer) Serialize(v any) (*nexus.Content, error) { var emptyReaderNopCloser = io.NopCloser(bytes.NewReader([]byte{})) -// statusGetter represents Temporal serviceerrors which have a Status() method. -type statusGetter interface { - Status() *status.Status +// convertKnownErrors converts a Temporal ApplicationError or a serviceerror to a Nexus HandlerError. +func convertKnownErrors(err error) error { + // Not using errors.As to be consistent ApplicationError checking with the rest of the SDK. + if appErr, ok := err.(*ApplicationError); ok { + if appErr.NonRetryable() { + return nexus.HandlerErrorf(nexus.HandlerErrorTypeBadRequest, appErr.Error()) + } + return nexus.HandlerErrorf(nexus.HandlerErrorTypeInternal, appErr.Error()) + } + // Default to expose details for now. We will likely make this configurable eventually. + return convertServiceError(err, true) } // convertServiceError converts a serviceerror into a Nexus HandlerError if possible. @@ -436,7 +442,11 @@ type statusGetter interface { // https://github.com/grpc-ecosystem/grpc-gateway/blob/a7cf811e6ffabeaddcfb4ff65602c12671ff326e/runtime/errors.go#L56. func convertServiceError(err error, exposeDetails bool) error { var st *status.Status - var stGetter statusGetter + + // Temporal serviceerrors have a Status() method. + var stGetter interface { + Status() *status.Status + } if !errors.As(err, &stGetter) { // Not a serviceerror, passthrough. return err @@ -499,16 +509,3 @@ func convertServiceError(err error, exposeDetails bool) error { } return err } - -// convertApplicationError converts a Temporal ApplicationError to a Nexus HandlerError, respecting the non_retryable -// flag. -func convertApplicationError(err error) error { - var appErr *ApplicationError - if errors.As(err, &appErr) { - if appErr.NonRetryable() { - return nexus.HandlerErrorf(nexus.HandlerErrorTypeBadRequest, appErr.Error()) - } - return nexus.HandlerErrorf(nexus.HandlerErrorTypeInternal, appErr.Error()) - } - return err -}