oto icon indicating copy to clipboard operation
oto copied to clipboard

Is there a way to do matching error on client side ?

Open fahmifan opened this issue 3 years ago • 2 comments

I'm trying oto and thanks it's made creating a client lib so easy. I wonder could oto create an error object for the client-side ? I imagine something like this

// definition.go
type ServiceError int

const (
	ErrInternal          ServiceError = 1000
	ErrPermissionDenined ServiceError = 1001
	ErrInvalidArgument   ServiceError = 1002
)

func (s ServiceError) Error() string {
	switch s {
	default: // ErrInternal
		return "internal"
        case ErrInvalidArgument:
                return "invalid_argument" 
	case ErrPermissionDenined:
		return "permission_denied"
	}
}
// client.js
const ErrInvalidArgument = new Error('permission_denied')
// index.js
try {
   res = await greeter.Greets(req)
} catch (err) {
   switch (err.message) {
    case greeter.ErrInvalidArgument:
        // do something
     case greeter.ErrPermissionDenined:
        // do something
     case greeter.ErrInternal:
        // do something
   }
}

fahmifan avatar Dec 11 '21 00:12 fahmifan

Hey @fahmifan, I could see a reason to do this. You could modify the template and make this for yourself, but do you think the standard Oto template should also do this as well?

matryer avatar Jan 25 '22 12:01 matryer

@matryer That would be great really great, if OTO has this feature built in! Unfortuantely i haven't got a chance to try implement the custom template my self, I'll try it if i got the chance

fahmifan avatar Jan 25 '22 13:01 fahmifan