Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bergundy committed Sep 9, 2024
1 parent 4c04d9a commit 0d7e474
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions internal/internal_nexus_task_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 0d7e474

Please sign in to comment.