-
-
Notifications
You must be signed in to change notification settings - Fork 52
Errors
Neffos separates a body from an error on its send and receive process. When Emit or Reply the other side's neffos.Message.Body
is filled with the provided data but if an event callback returns a non-nil error then the incoming neffos.Message.Err
is set to a standard go error
filled with the remote side's error text. However, depending on the application requirements, you may want to specify and give more meaning to those errors, i.e be able to compare them with local events like you do with io.EOF
which may be returned from an io.Reader
.
To support meaningful errors in the same application, each side that needs to compare local errors with remote ones, must mark them as "known" errors via the package-level RegisterKnownError(err error)
function. All known errors will under-the-hoods be compared with an incoming error text and if match then this error is set to the neffos.Message.Err
field, therefore the caller can directly compare between knwonError == msg.Err
. For dynamic error (that its Error() string
can differ based on a runtime condition) the RegisterKnownError
handles a special case of error contains a ResolveError(errorText string) bool
method.
Given the following sample:
// myapp/shared/errors.go
import "errors"
import "github.com/kataras/neffos"
var MyError = errors.New("my error")
func init() {
// Now the 'MyError' can be compared against
// a server or client side's incoming Message.Err field.
neffos.RegisterKnownError(MyError)
}
// myapp/server/server.go
import "myapp/shared"
import "github.com/kataras/neffos"
func onSomething(c *neffos.NSConn, msg neffos.Message) error {
if somethingBad {
// send the MyError as a response
// of this namespace's event to the client side.
return shared.MyError
}
// [...]
return nil
}
// myapp/client/client.go
import "myapp/shared"
import "github.com/kataras/neffos"
func onSomething(c *neffos.NSConn, msg neffos.Message) error {
if msg.Err != nil {
if msg.Err == shared.MyError {
// [handle MyError coming from server to client...]
}
// [handle other error...]
}
// [...]
return nil
}
Read the Wait for response section for a more comprehensive usage of that kind of errors.
Continue by reading about Namespaces.
Home | About | Project | Getting Started | Technical Docs | Copyright © 2019-2023 Gerasimos Maropoulos. Documentation terms of use.