Skip to content

Commit

Permalink
#871 define a error represents service implementation error
Browse files Browse the repository at this point in the history
  • Loading branch information
smallnest committed Aug 22, 2024
1 parent 4ab6503 commit 8914b35
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ import (
"github.com/smallnest/rpcx/log"
)

// RpcServiceError represents an error that is case by service implementation.
type RpcServiceInternalError struct {
Err string
Method string
Argv interface{}
stack string
}

// Error returns the error message.
func (e RpcServiceInternalError) Error() string {
return fmt.Sprintf("[service internal error]: %v, method: %s, argv: %+v, stack: %s", e.Err, e.Method, e.Argv, e.stack)
}

// String returns the error message.
func (e RpcServiceInternalError) String() string {
return e.Error()
}

// Precompute the reflect type for error. Can't use error directly
// because Typeof takes an empty interface value. This is annoying.
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
Expand Down Expand Up @@ -357,8 +375,12 @@ func (s *service) call(ctx context.Context, mtype *methodType, argv, replyv refl
n := runtime.Stack(buf, false)
buf = buf[:n]

err = fmt.Errorf("[service internal error]: %v, method: %s, argv: %+v, stack: %s",
r, mtype.method.Name, argv.Interface(), buf)
err = &RpcServiceInternalError{
Err: fmt.Sprintf("%v", r),
Method: mtype.method.Name,
Argv: argv.Interface(),
stack: string(buf),
}
log.Error(err)
}
}()
Expand All @@ -382,9 +404,12 @@ func (s *service) callForFunction(ctx context.Context, ft *functionType, argv, r
n := runtime.Stack(buf, false)
buf = buf[:n]

// log.Errorf("failed to invoke service: %v, stacks: %s", r, string(debug.Stack()))
err = fmt.Errorf("[service internal error]: %v, function: %s, argv: %+v, stack: %s",
r, runtime.FuncForPC(ft.fn.Pointer()), argv.Interface(), buf)
err = &RpcServiceInternalError{
Err: fmt.Sprintf("%v", r),
Method: fmt.Sprintf("%s", runtime.FuncForPC(ft.fn.Pointer())),
Argv: argv.Interface(),
stack: string(buf),
}
log.Error(err)
}
}()
Expand Down

0 comments on commit 8914b35

Please sign in to comment.