# How to handle error in go grpc service?

**URL:** <https://forum.golangbridge.org/t/how-to-handle-error-in-go-grpc-service/15021>\
**Category:** Getting Help\
**Created:** [August 22, 2019, 2:22am UTC](https://forum.golangbridge.org/t/how-to-handle-error-in-go-grpc-service/15021 "2019-08-22T02:22:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![root666](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/root666/32/4166_2.png) [@root666](https://forum.golangbridge.org/u/root666)\
**Post date:** [August 22, 2019, 2:22am UTC](https://forum.golangbridge.org/t/how-to-handle-error-in-go-grpc-service/15021/1 "2019-08-22T02:22:59Z")

</div>

say we have several grpc services written in go, like

service 1

```go
func (s *srv1) Foo(req Request, rsp *Response) error {
    // handle logic but find some error
    return err
}

```

service2

```go
func (s *srv2) Bar(req Request, rsp *Response) error {
    // handle logic but find some error
    rsp.Code = Err_Code
    rsp.Msg = err.Error()
    return nil
}

```

service3

```go
func (s *srv3) Biz(req Request, rsp *Response) error {
    // handle logic and find no error
    return nil
}

```

and here we have a java client calling our go services:

```auto
try {
    srv1.Foo()
    srv2.Bar()
    srv3.Biz()
} catch (Exception e) {
   // handle exception
}

```

In our case, if `srv1` returns error an exception will be caught which cause `srv2` and `srv3` not being called. So my question is, should our service return error which cause the clients terminated, or should our service always return nil but give `rsp.Code` and `rsp.Msg` instead which enable the clients to call `srv2` and `srv3` even if `srv1` failed ? or there are some better ways for the server and client to handle errors?

sorry for my english, thanks for helping 🙂

---

<div class="post-metadata">

**Author:** ![iegomez](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/iegomez/32/3643_2.png) [@iegomez](https://forum.golangbridge.org/u/iegomez)\
**Post date:** [August 22, 2019, 2:23pm UTC](https://forum.golangbridge.org/t/how-to-handle-error-in-go-grpc-service/15021/2 "2019-08-22T14:23:54Z")

</div>

For starters, this has nothing to do with Go but with gRPC, so I don’t think it’s the best place to be discussing it. Anyway, my 2 cents:

> [@root666](#):
>
> In our case, if `srv1` returns error an exception will be caught which cause `srv2` and `srv3` not being called.

That’s because you’re throwing everything inside the same try, which isn’t necessary. If you think you can go on calling next services after one failed, then handle each one separately. It’s not that “_`rsp.Code` and `rsp.Msg` instead … enable the clients to call `srv2` and `srv3` even if `srv1` failed_”, is how you handle the errors that enables the subsequent calls: if you actually got an `Err_Code` from `srv1` (with a `nil` error), how would you handle it and would you continue to call the other services? That’s logic that depends on your application.

On the other hand, what’s the point of having a function return an error if you’ll always return `nil`?

For what is worth, I would return errors from the services and then handle them properly at the client.

---

<div class="post-metadata">

**Author:** ![root666](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/root666/32/4166_2.png) [@root666](https://forum.golangbridge.org/u/root666)\
**Post date:** [September 3, 2019, 8:44am UTC](https://forum.golangbridge.org/t/how-to-handle-error-in-go-grpc-service/15021/3 "2019-09-03T08:44:02Z")

</div>

thanks, agree with you, but temporarily our team choose to always return nil and use code+msg instead 😔 maybe someday find a better solution…

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [December 2, 2019, 8:44am UTC](https://forum.golangbridge.org/t/how-to-handle-error-in-go-grpc-service/15021/4 "2019-12-02T08:44:04Z")

</div>

Closed on December 2, 2019, 8:44am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
