@rivascva/dt-idl 1.1.46 → 1.1.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -8,19 +8,19 @@ import (
|
|
|
8
8
|
"github.com/RivasCVA/dt-idl/go/utils/logger"
|
|
9
9
|
)
|
|
10
10
|
|
|
11
|
-
type
|
|
11
|
+
type Responder[T any] interface {
|
|
12
12
|
Write(w http.ResponseWriter, status int, data any)
|
|
13
13
|
WriteError(w http.ResponseWriter, status int, message string)
|
|
14
14
|
WriteErrorWithCode(w http.ResponseWriter, status int, code T, message string)
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
type
|
|
17
|
+
type responder[T any] struct {
|
|
18
18
|
log logger.Logger
|
|
19
19
|
baseErrorCode T
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
func
|
|
23
|
-
return &
|
|
22
|
+
func NewResponder[T any](log logger.Logger, baseErrorCode T) Responder[T] {
|
|
23
|
+
return &responder[T]{
|
|
24
24
|
log: log,
|
|
25
25
|
baseErrorCode: baseErrorCode,
|
|
26
26
|
}
|
|
@@ -29,7 +29,7 @@ func New[T any](log logger.Logger, baseErrorCode T) Response[T] {
|
|
|
29
29
|
// Writes a JSON response to the given response writer.
|
|
30
30
|
//
|
|
31
31
|
// The given data is converted to JSON. An error response is written if the marshal fails.
|
|
32
|
-
func (r *
|
|
32
|
+
func (r *responder[T]) Write(w http.ResponseWriter, status int, data any) {
|
|
33
33
|
// encode the given data object
|
|
34
34
|
out, err := json.Marshal(data)
|
|
35
35
|
if err != nil {
|
|
@@ -49,7 +49,7 @@ func (r *response[T]) Write(w http.ResponseWriter, status int, data any) {
|
|
|
49
49
|
// Writes a JSON error response to the given response writer.
|
|
50
50
|
//
|
|
51
51
|
// The response is based on the generated "api.Error" schema.
|
|
52
|
-
func (r *
|
|
52
|
+
func (r *responder[T]) WriteError(w http.ResponseWriter, status int, message string) {
|
|
53
53
|
r.WriteErrorWithCode(w, status, r.baseErrorCode, message)
|
|
54
54
|
}
|
|
55
55
|
|
|
@@ -58,7 +58,7 @@ func (r *response[T]) WriteError(w http.ResponseWriter, status int, message stri
|
|
|
58
58
|
// The code is based on the generated "api.ErrorCode" enum.
|
|
59
59
|
//
|
|
60
60
|
// The response is based on the generated "api.Error" schema.
|
|
61
|
-
func (r *
|
|
61
|
+
func (r *responder[T]) WriteErrorWithCode(w http.ResponseWriter, status int, code T, message string) {
|
|
62
62
|
// encode an error object with the given message
|
|
63
63
|
out, err := json.Marshal(Error[T]{
|
|
64
64
|
Status: int64(status),
|