Error prepares and publishes error response from a handler. A response error should be set containing an error code and description. Optionally, data can be set as response payload.
(code, description string, data []byte, opts ...RespondOpt)
| 131 | // A response error should be set containing an error code and description. |
| 132 | // Optionally, data can be set as response payload. |
| 133 | func (r *request) Error(code, description string, data []byte, opts ...RespondOpt) error { |
| 134 | if code == "" { |
| 135 | return fmt.Errorf("%w: error code", ErrArgRequired) |
| 136 | } |
| 137 | if description == "" { |
| 138 | return fmt.Errorf("%w: description", ErrArgRequired) |
| 139 | } |
| 140 | response := &nats.Msg{ |
| 141 | Header: nats.Header{ |
| 142 | ErrorHeader: []string{description}, |
| 143 | ErrorCodeHeader: []string{code}, |
| 144 | }, |
| 145 | } |
| 146 | for _, opt := range opts { |
| 147 | opt(response) |
| 148 | } |
| 149 | |
| 150 | response.Data = data |
| 151 | if err := r.msg.RespondMsg(response); err != nil { |
| 152 | r.respondError = err |
| 153 | return err |
| 154 | } |
| 155 | r.respondError = &serviceError{ |
| 156 | Code: code, |
| 157 | Description: description, |
| 158 | } |
| 159 | |
| 160 | return nil |
| 161 | } |
| 162 | |
| 163 | // WithHeaders can be used to configure response with custom headers. |
| 164 | func WithHeaders(headers Headers) RespondOpt { |
nothing calls this directly
no test coverage detected