all repos

onasty @ 36f59cd12d0992b16f20057c23cfe0e3c338e637

a one-time notes service

onasty/internal/events/events.go (view raw)

Smirnov Oleksandr Smirnov Oleksandr
ss2316544@gmail.com
refactor: fix annoyances (#97)..., 1 year ago
1
package events
2
3
import (
4
	"fmt"
5
6
	"github.com/nats-io/nats.go"
7
)
8
9
const (
10
	natsHeaderErrorCode = "Nats-Service-Error-Code"
11
	natsHeaderErrorMsg  = "Nats-Service-Error"
12
)
13
14
type Error struct {
15
	Code    string
16
	Message string
17
}
18
19
func (e Error) Error() string {
20
	return fmt.Sprintf("code: %s; msg: %s", e.Code, e.Message)
21
}
22
23
func CheckRespForError(resp *nats.Msg) error {
24
	code := resp.Header.Get(natsHeaderErrorCode)
25
	msg := resp.Header.Get(natsHeaderErrorMsg)
26
	if code == "" && msg == "" {
27
		return nil
28
	}
29
30
	return &Error{
31
		Code:    code,
32
		Message: msg,
33
	}
34
}