mirror of
https://github.com/taigrr/nats.docs
synced 2025-01-18 04:03:23 -08:00
structured data: Go: handle decode errors
The automatic handling of encoding/decoding of structured data can fail, if the data is not in the expected format. For Go, show how this is handled with a connection error handler option.
This commit is contained in:
parent
94db70a982
commit
e0ad105373
@ -7,7 +7,10 @@ For example, to receive JSON you could do:
|
|||||||
{% tabs %}
|
{% tabs %}
|
||||||
{% tab title="Go" %}
|
{% tab title="Go" %}
|
||||||
```go
|
```go
|
||||||
nc, err := nats.Connect("demo.nats.io")
|
nc, err := nats.Connect("demo.nats.io",
|
||||||
|
nats.ErrorHandler(func(nc *nats.Conn, s *nats.Subscription, err error) {
|
||||||
|
log.Printf("Async error in %q/%q: %v", s.Subject, s.Queue, err)
|
||||||
|
}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -28,6 +31,9 @@ wg := sync.WaitGroup{}
|
|||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
|
||||||
// Subscribe
|
// Subscribe
|
||||||
|
// Decoding errors will be passed to the function supplied via
|
||||||
|
// nats.ErrorHandler above, and the callback supplied here will
|
||||||
|
// not be invoked.
|
||||||
if _, err := ec.Subscribe("updates", func(s *stock) {
|
if _, err := ec.Subscribe("updates", func(s *stock) {
|
||||||
log.Printf("Stock: %s - Price: %v", s.Symbol, s.Price)
|
log.Printf("Stock: %s - Price: %v", s.Symbol, s.Price)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user