Skip to content

Commit f7281c2

Browse files
committed
Refactor error handling to use fmt.Errorf instead of catcher for improved clarity
1 parent 0046106 commit f7281c2

File tree

10 files changed

+38
-68
lines changed

10 files changed

+38
-68
lines changed

utils/casts.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package utils
22

33
import (
44
"fmt"
5-
"github.com/threatwinds/go-sdk/catcher"
65
"reflect"
76
"strconv"
87
)
@@ -32,7 +31,6 @@ func CastInt64(value interface{}) int64 {
3231
case string:
3332
val, err := strconv.ParseInt(v, 10, 64)
3433
if err != nil {
35-
_ = catcher.Error("failed to cast string to int64", err, map[string]any{"value": v})
3634
return 0
3735
}
3836
return val
@@ -66,7 +64,6 @@ func CastFloat64(value interface{}) float64 {
6664
case string:
6765
val, err := strconv.ParseFloat(v, 64)
6866
if err != nil {
69-
_ = catcher.Error("failed to cast string to float64", err, map[string]any{"value": v})
7067
return 0
7168
}
7269
return val
@@ -103,7 +100,6 @@ func CastBool(value interface{}) bool {
103100
case string:
104101
val, err := strconv.ParseBool(v)
105102
if err != nil {
106-
_ = catcher.Error("failed to cast string to bool", err, map[string]any{"value": v})
107103
return false
108104
}
109105
return val

utils/csv.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package utils
22

33
import (
44
"encoding/csv"
5-
"github.com/threatwinds/go-sdk/catcher"
5+
"fmt"
66
"os"
77
)
88

@@ -18,14 +18,14 @@ import (
1818
func ReadCSV(url string) ([][]string, error) {
1919
file, err := os.Open(url)
2020
if err != nil {
21-
return nil, catcher.Error("error opening CSV file", err, map[string]any{"file": url})
21+
return nil, fmt.Errorf("error opening CSV file %s: %w", url, err)
2222
}
2323
defer func() { _ = file.Close() }()
2424

2525
reader := csv.NewReader(file)
2626
result, err := reader.ReadAll()
2727
if err != nil {
28-
return nil, catcher.Error("error reading CSV file", err, map[string]any{"file": url})
28+
return nil, fmt.Errorf("error reading CSV file %s: %w", url, err)
2929
}
3030

3131
return result, nil

utils/fields.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package utils
22

33
import (
4-
"errors"
5-
"github.com/threatwinds/go-sdk/catcher"
4+
"fmt"
65
"regexp"
76
"strings"
87
)
@@ -15,13 +14,12 @@ func ValidateReservedField(f string, allowEmpty bool) error {
1514
}
1615

1716
if f == "" && !allowEmpty {
18-
return catcher.Error("error validating field", errors.New("field name cannot be empty"), nil)
17+
return fmt.Errorf("error validating field: field name cannot be empty")
1918
}
2019

2120
for _, rf := range reservedFields {
2221
if f == rf {
23-
return catcher.Error("error validating field", errors.New("field cannot be a reserved field"),
24-
map[string]any{"reservedFields": reservedFields, "usedField": f})
22+
return fmt.Errorf("error validating field: field cannot be a reserved field (reserved: %v, used: %s)", reservedFields, f)
2523
}
2624
}
2725

@@ -37,7 +35,6 @@ func SanitizeField(s *string) {
3735
// compile the pattern
3836
compiledPattern, err := regexp.Compile(exp)
3937
if err != nil {
40-
_ = catcher.Error("error compiling regexp", err, nil)
4138
return
4239
}
4340

utils/files.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package utils
22

33
import (
4-
"github.com/threatwinds/go-sdk/catcher"
4+
"fmt"
55
"strings"
66
)
77

@@ -18,19 +18,13 @@ func ValidateFilePath(path string) error {
1818

1919
for _, c := range contains {
2020
if strings.Contains(path, c) {
21-
return catcher.Error("path contains an invalid character", nil, map[string]any{
22-
"path": path,
23-
"invalid": c,
24-
})
21+
return fmt.Errorf("path contains an invalid character: path=%s, invalid=%s", path, c)
2522
}
2623
}
2724

2825
for _, p := range prefixes {
2926
if strings.HasPrefix(path, p) {
30-
return catcher.Error("path starts with an invalid character", nil, map[string]any{
31-
"path": path,
32-
"invalid": p,
33-
})
27+
return fmt.Errorf("path starts with an invalid character: path=%s, invalid=%s", path, p)
3428
}
3529
}
3630

utils/json.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package utils
22

33
import (
44
"encoding/json"
5-
"github.com/threatwinds/go-sdk/catcher"
5+
"fmt"
66
"os"
77
)
88

@@ -25,14 +25,14 @@ import (
2525
func ReadJSON[t any](f string) (*t, error) {
2626
content, err := os.ReadFile(f)
2727
if err != nil {
28-
return nil, catcher.Error("error reading JSON file", err, map[string]any{"file": f})
28+
return nil, fmt.Errorf("error reading JSON file %s: %w", f, err)
2929
}
3030

3131
var value = new(t)
3232

3333
err = json.Unmarshal(content, value)
3434
if err != nil {
35-
return nil, catcher.Error("error parsing JSON file", err, map[string]any{"file": f})
35+
return nil, fmt.Errorf("error parsing JSON file %s: %w", f, err)
3636
}
3737

3838
return value, nil

utils/list_files.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package utils
22

33
import (
4-
"github.com/threatwinds/go-sdk/catcher"
4+
"fmt"
55
"os"
66
"path/filepath"
77
"strings"
@@ -33,7 +33,7 @@ func ListFiles(route string, filter string) []string {
3333
})
3434
if err != nil {
3535
if !strings.Contains(err.Error(), "no such file or directory") {
36-
panic(catcher.Error("cannot walk through directory", err, map[string]any{"route": route}))
36+
panic(fmt.Errorf("cannot walk through directory %s: %w", route, err))
3737
}
3838
}
3939

utils/network.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package utils
22

33
import (
44
"context"
5-
"github.com/threatwinds/go-sdk/catcher"
5+
"fmt"
66
"net"
77
"time"
88
)
@@ -23,19 +23,19 @@ func GetMainIP() (string, error) {
2323
var d net.Dialer
2424
conn, err := d.DialContext(ctx, "udp", "8.8.8.8:80")
2525
if err != nil {
26-
return "", catcher.Error("failed to create Dial context", err, nil)
26+
return "", fmt.Errorf("failed to create Dial context: %w", err)
2727
}
2828
defer func() {
2929
_ = conn.Close()
3030
}()
3131

3232
localAddr, ok := conn.LocalAddr().(*net.UDPAddr)
3333
if !ok {
34-
return "", catcher.Error("failed to get local address", nil, nil)
34+
return "", fmt.Errorf("failed to get local address: invalid type")
3535
}
3636

3737
if localAddr.IP == nil {
38-
return "", catcher.Error("failed to get local IP address", nil, nil)
38+
return "", fmt.Errorf("failed to get local IP address: IP is nil")
3939
}
4040

4141
return localAddr.IP.String(), nil

utils/protojson.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package utils
22

33
import (
4-
"errors"
54
"fmt"
6-
"github.com/threatwinds/go-sdk/catcher"
75
"google.golang.org/protobuf/encoding/protojson"
86
"google.golang.org/protobuf/reflect/protoreflect"
97
)
@@ -21,12 +19,12 @@ const maxMessageSize = 10 * 1024 * 1024 // 10MB limit
2119
// - *error: A pointer to an error if an error occurs, otherwise nil.
2220
func ToString(object protoreflect.ProtoMessage) (*string, error) {
2321
if object == nil {
24-
return nil, catcher.Error("cannot convert to string", errors.New("object is a nil pointer"), nil)
22+
return nil, fmt.Errorf("cannot convert to string: object is a nil pointer")
2523
}
2624

2725
objectBytes, err := protojson.Marshal(object)
2826
if err != nil {
29-
return nil, catcher.Error("cannot convert to string", err, nil)
27+
return nil, fmt.Errorf("cannot convert to string: %w", err)
3028
}
3129

3230
objectString := string(objectBytes)
@@ -44,24 +42,18 @@ func ToString(object protoreflect.ProtoMessage) (*string, error) {
4442
// - error: An error object if the unmarshalling fails, otherwise nil.
4543
func ToObject(str *string, object protoreflect.ProtoMessage) error {
4644
if str == nil || object == nil {
47-
return catcher.Error("cannot convert to object", errors.New("object or string is a nil pointer"), map[string]any{
48-
"nilStr": str == nil,
49-
"nilObject": object == nil,
50-
})
45+
return fmt.Errorf("cannot convert to object: object or string is a nil pointer (nilStr=%v, nilObject=%v)", str == nil, object == nil)
5146
}
5247

5348
if len(*str) > maxMessageSize {
54-
return catcher.Error("cannot convert to object", errors.New("message size exceeds limit"), map[string]any{
55-
"size": fmt.Sprintf("%d bytes", len(*str)),
56-
"limit": fmt.Sprintf("%d bytes", maxMessageSize),
57-
})
49+
return fmt.Errorf("cannot convert to object: message size exceeds limit (size=%d bytes, limit=%d bytes)", len(*str), maxMessageSize)
5850
}
5951

6052
unmarshaler := protojson.UnmarshalOptions{DiscardUnknown: true}
6153

6254
err := unmarshaler.Unmarshal([]byte(*str), object)
6355
if err != nil {
64-
return catcher.Error("failed to parse object", err, nil)
56+
return fmt.Errorf("failed to parse object: %w", err)
6557
}
6658

6759
return nil

utils/request.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import (
44
"bytes"
55
"crypto/tls"
66
"encoding/json"
7-
"errors"
87
"fmt"
9-
"github.com/threatwinds/go-sdk/catcher"
108
"io"
119
"net/http"
1210
"os"
@@ -38,16 +36,12 @@ func DoReq[response any](url string, data []byte, method string, headers map[str
3836
var result response
3937

4038
if len(data) > maxMessageSize {
41-
return result, http.StatusBadRequest, catcher.Error("cannot convert to object",
42-
errors.New("data size exceeds limit"), map[string]any{
43-
"size": fmt.Sprintf("%d bytes", len(data)),
44-
"limit": fmt.Sprintf("%d bytes", maxMessageSize),
45-
})
39+
return result, http.StatusBadRequest, fmt.Errorf("cannot convert to object: data size exceeds limit (size=%d bytes, limit=%d bytes)", len(data), maxMessageSize)
4640
}
4741

4842
req, err := http.NewRequest(method, url, bytes.NewBuffer(data))
4943
if err != nil {
50-
return result, http.StatusInternalServerError, catcher.Error("error creating request", err, nil)
44+
return result, http.StatusInternalServerError, fmt.Errorf("error creating request: %w", err)
5145
}
5246

5347
for k, v := range headers {
@@ -67,21 +61,18 @@ func DoReq[response any](url string, data []byte, method string, headers map[str
6761

6862
resp, err := client.Do(req)
6963
if err != nil {
70-
return result, http.StatusInternalServerError, catcher.Error("error doing request", err, nil)
64+
return result, http.StatusInternalServerError, fmt.Errorf("error doing request: %w", err)
7165
}
7266

7367
defer func() { _ = resp.Body.Close() }()
7468

7569
body, err := io.ReadAll(resp.Body)
7670
if err != nil {
77-
return result, http.StatusInternalServerError, catcher.Error("error reading response body", err, nil)
71+
return result, http.StatusInternalServerError, fmt.Errorf("error reading response body: %w", err)
7872
}
7973

8074
if resp.StatusCode >= 400 {
81-
return result, resp.StatusCode, catcher.Error("error response", nil, map[string]interface{}{
82-
"response": string(body),
83-
"status": resp.StatusCode,
84-
})
75+
return result, resp.StatusCode, fmt.Errorf("error response (status=%d): %s", resp.StatusCode, string(body))
8576
}
8677

8778
if resp.StatusCode == http.StatusNoContent {
@@ -90,7 +81,7 @@ func DoReq[response any](url string, data []byte, method string, headers map[str
9081

9182
err = json.Unmarshal(body, &result)
9283
if err != nil {
93-
return result, resp.StatusCode, catcher.Error("error parsing response", err, nil)
84+
return result, resp.StatusCode, fmt.Errorf("error parsing response: %w", err)
9485
}
9586

9687
return result, resp.StatusCode, nil
@@ -108,7 +99,7 @@ func DoReq[response any](url string, data []byte, method string, headers map[str
10899
func Download(url, file string) error {
109100
out, err := os.Create(file)
110101
if err != nil {
111-
return catcher.Error("error creating file", err, map[string]interface{}{"file": file})
102+
return fmt.Errorf("error creating file %s: %w", file, err)
112103
}
113104

114105
defer func() { _ = out.Close() }()
@@ -125,14 +116,14 @@ func Download(url, file string) error {
125116

126117
resp, err := client.Get(url)
127118
if err != nil {
128-
return catcher.Error("error downloading file", err, map[string]any{"url": url})
119+
return fmt.Errorf("error downloading file from %s: %w", url, err)
129120
}
130121

131122
defer func() { _ = resp.Body.Close() }()
132123

133124
_, err = io.Copy(out, resp.Body)
134125
if err != nil {
135-
return catcher.Error("error saving file", err, map[string]any{"file": file})
126+
return fmt.Errorf("error saving file %s: %w", file, err)
136127
}
137128

138129
return nil

utils/yaml.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package utils
22

33
import (
4-
"github.com/threatwinds/go-sdk/catcher"
4+
"fmt"
55
"os"
66

77
"gopkg.in/yaml.v3"
@@ -20,12 +20,12 @@ import (
2020
func ReadPbYaml(f string) ([]byte, error) {
2121
content, err := os.ReadFile(f)
2222
if err != nil {
23-
return nil, catcher.Error("error opening file", err, map[string]interface{}{"file": f})
23+
return nil, fmt.Errorf("error opening file %s: %w", f, err)
2424
}
2525

2626
bytes, err := k8syaml.YAMLToJSON(content)
2727
if err != nil {
28-
return nil, catcher.Error("error converting YAML to JSON", err, map[string]interface{}{"file": f})
28+
return nil, fmt.Errorf("error converting YAML to JSON for file %s: %w", f, err)
2929
}
3030

3131
return bytes, nil
@@ -50,19 +50,19 @@ func ReadPbYaml(f string) ([]byte, error) {
5050
func ReadYaml[t any](f string, jsonMode bool) (*t, error) {
5151
content, err := os.ReadFile(f)
5252
if err != nil {
53-
return nil, catcher.Error("error opening file", err, map[string]any{"file": f})
53+
return nil, fmt.Errorf("error opening file %s: %w", f, err)
5454
}
5555

5656
var value = new(t)
5757
if jsonMode {
5858
err = k8syaml.Unmarshal(content, value)
5959
if err != nil {
60-
return nil, catcher.Error("error decoding file", err, map[string]any{"file": f})
60+
return nil, fmt.Errorf("error decoding file %s: %w", f, err)
6161
}
6262
} else {
6363
err = yaml.Unmarshal(content, value)
6464
if err != nil {
65-
return nil, catcher.Error("error decoding file", err, map[string]any{"file": f})
65+
return nil, fmt.Errorf("error decoding file %s: %w", f, err)
6666
}
6767
}
6868

0 commit comments

Comments
 (0)