Skip to content

Commit 8914b35

Browse files
committed
#871 define a error represents service implementation error
1 parent 4ab6503 commit 8914b35

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

โ€Žserver/service.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,24 @@ import (
1515
"github.com/smallnest/rpcx/log"
1616
)
1717

18+
// RpcServiceError represents an error that is case by service implementation.
19+
type RpcServiceInternalError struct {
20+
Err string
21+
Method string
22+
Argv interface{}
23+
stack string
24+
}
25+
26+
// Error returns the error message.
27+
func (e RpcServiceInternalError) Error() string {
28+
return fmt.Sprintf("[service internal error]: %v, method: %s, argv: %+v, stack: %s", e.Err, e.Method, e.Argv, e.stack)
29+
}
30+
31+
// String returns the error message.
32+
func (e RpcServiceInternalError) String() string {
33+
return e.Error()
34+
}
35+
1836
// Precompute the reflect type for error. Can't use error directly
1937
// because Typeof takes an empty interface value. This is annoying.
2038
var typeOfError = reflect.TypeOf((*error)(nil)).Elem()
@@ -357,8 +375,12 @@ func (s *service) call(ctx context.Context, mtype *methodType, argv, replyv refl
357375
n := runtime.Stack(buf, false)
358376
buf = buf[:n]
359377

360-
err = fmt.Errorf("[service internal error]: %v, method: %s, argv: %+v, stack: %s",
361-
r, mtype.method.Name, argv.Interface(), buf)
378+
err = &RpcServiceInternalError{
379+
Err: fmt.Sprintf("%v", r),
380+
Method: mtype.method.Name,
381+
Argv: argv.Interface(),
382+
stack: string(buf),
383+
}
362384
log.Error(err)
363385
}
364386
}()
@@ -382,9 +404,12 @@ func (s *service) callForFunction(ctx context.Context, ft *functionType, argv, r
382404
n := runtime.Stack(buf, false)
383405
buf = buf[:n]
384406

385-
// log.Errorf("failed to invoke service: %v, stacks: %s", r, string(debug.Stack()))
386-
err = fmt.Errorf("[service internal error]: %v, function: %s, argv: %+v, stack: %s",
387-
r, runtime.FuncForPC(ft.fn.Pointer()), argv.Interface(), buf)
407+
err = &RpcServiceInternalError{
408+
Err: fmt.Sprintf("%v", r),
409+
Method: fmt.Sprintf("%s", runtime.FuncForPC(ft.fn.Pointer())),
410+
Argv: argv.Interface(),
411+
stack: string(buf),
412+
}
388413
log.Error(err)
389414
}
390415
}()

0 commit comments

Comments
ย (0)