Skip to content

Commit 5a36088

Browse files
committed
Wrap package errors.
1 parent e89dd6a commit 5a36088

File tree

7 files changed

+160
-14
lines changed

7 files changed

+160
-14
lines changed

internal/client/aws.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (c awsClient) GetValues(ctx context.Context, ignoreNotFound bool) (values.V
8585
if ignoreNotFound && errors.As(err, &notFoundErr) {
8686
continue
8787
}
88-
return nil, err
88+
return nil, gettingValueError(v.Name, err)
8989
}
9090

9191
if err := values.SetValue(v, output.Parameter.Value); err != nil {
@@ -103,7 +103,7 @@ func (c awsClient) GetValues(ctx context.Context, ignoreNotFound bool) (values.V
103103
if ignoreNotFound && errors.As(err, &notFoundErr) {
104104
continue
105105
}
106-
return nil, err
106+
return nil, gettingValueError(v.Name, err)
107107
}
108108

109109
if err := values.SetValue(v, output.SecretString); err != nil {

internal/client/cache/cache.go

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828

2929
var (
3030
ErrInvalidCacheType = errors.New("invalid cache type")
31+
ErrCacheProcessing = errors.New("cache processing error")
3132
)
3233

3334
type Cache interface {
@@ -58,3 +59,11 @@ func New(cfg config.CacheConfig, options ...Option) (Cache, error) {
5859
func keyToHex(key string) string {
5960
return hex.EncodeToString([]byte(key))
6061
}
62+
63+
func cacheProcessingError(msg string, err error) error {
64+
if err != nil {
65+
return fmt.Errorf("%w: %s: %w", ErrCacheProcessing, msg, err)
66+
}
67+
68+
return fmt.Errorf("%w: %s", ErrCacheProcessing, msg)
69+
}

internal/client/cache/file.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"crypto/aes"
2222
"crypto/cipher"
2323
"crypto/rand"
24-
"errors"
2524
"io"
2625
"os"
2726
"path/filepath"
@@ -66,7 +65,7 @@ func newFileCache(cfg config.FileCacheConfig, expireDuration time.Duration, opti
6665

6766
// create cache directory
6867
if err := os.MkdirAll(fc.cachePath, 0777); err != nil {
69-
return nil, err
68+
return nil, cacheProcessingError("failed to create cache directory", err)
7069
}
7170

7271
// read or create key
@@ -77,7 +76,7 @@ func newFileCache(cfg config.FileCacheConfig, expireDuration time.Duration, opti
7776

7877
fc.cipherBlock, err = aes.NewCipher(key)
7978
if err != nil {
80-
return nil, err
79+
return nil, cacheProcessingError("failed to create cipher block", err)
8180
}
8281

8382
return fc, nil
@@ -145,7 +144,7 @@ func (f *fileCache) readOrCreateKey() ([]byte, error) {
145144

146145
// create key file
147146
if _, err := rand.Read(key); err != nil {
148-
return nil, err
147+
return nil, cacheProcessingError("failed to create key", err)
149148
}
150149
if err := f.writeToFile(keyFileName, key, false); err != nil {
151150
return nil, err
@@ -178,15 +177,15 @@ func (f *fileCache) readOrCreateKey() ([]byte, error) {
178177

179178
// check key is not tampered
180179
if err := bcrypt.CompareHashAndPassword(keyHash, key); err != nil {
181-
return nil, err
180+
return nil, cacheProcessingError("key is tampered", err)
182181
}
183182

184183
return key, nil
185184
}
186185

187186
func (f *fileCache) decryptCache(cipherText []byte) ([]byte, error) {
188187
if len(cipherText) < aes.BlockSize {
189-
return nil, errors.New("ciphertext too short")
188+
return nil, cacheProcessingError("cipher text is too short", nil)
190189
}
191190

192191
iv := cipherText[:aes.BlockSize]
@@ -202,7 +201,7 @@ func (f *fileCache) encryptCache(plainText []byte) ([]byte, error) {
202201
cipherText := make([]byte, aes.BlockSize+len(plainText))
203202
iv := cipherText[:aes.BlockSize]
204203
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
205-
return nil, err
204+
return nil, cacheProcessingError("failed to create initialization vector", err)
206205
}
207206

208207
stream := cipher.NewCFBEncrypter(f.cipherBlock, iv)
@@ -228,7 +227,7 @@ func (f fileCache) readFile(filename string, hidden bool) ([]byte, error) {
228227

229228
data, err := os.ReadFile(filepath.Join(f.cachePath, filename))
230229
if err != nil {
231-
return nil, err
230+
return nil, cacheProcessingError("failed to read file", err)
232231
}
233232
data = data[:len(data)-1]
234233

@@ -243,7 +242,7 @@ func (f fileCache) writeToFile(filename string, data []byte, hidden bool) error
243242

244243
file, err := os.Create(filepath.Join(f.cachePath, filename))
245244
if err != nil {
246-
return err
245+
return cacheProcessingError("failed to create file", err)
247246
}
248247
defer file.Close()
249248

internal/client/client.go

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package client
1919
import (
2020
"context"
2121
"errors"
22+
"fmt"
2223

2324
"github.com/dwango/yashiro/internal/values"
2425
"github.com/dwango/yashiro/pkg/config"
@@ -27,6 +28,7 @@ import (
2728
// Define errors
2829
var (
2930
ErrNotfoundValueConfig = errors.New("not found value config")
31+
ErrGettingValue = errors.New("failed to get value")
3032
)
3133

3234
// Client is the external stores client.
@@ -52,3 +54,7 @@ func New(cfg *config.Config) (Client, error) {
5254

5355
return client, nil
5456
}
57+
58+
func gettingValueError(name string, err error) error {
59+
return fmt.Errorf("%w: name='%s': %w", ErrGettingValue, name, err)
60+
}

pkg/engine/engine.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package engine
1919
import (
2020
"bytes"
2121
"context"
22+
"errors"
23+
"fmt"
2224
"io"
2325
"text/template"
2426

@@ -27,6 +29,10 @@ import (
2729
"github.com/dwango/yashiro/pkg/engine/encoding"
2830
)
2931

32+
var (
33+
ErrRendering = errors.New("failed to render")
34+
)
35+
3036
// Engine is a template engine.
3137
type Engine interface {
3238
Render(ctx context.Context, text string, dest io.Writer) error
@@ -81,12 +87,12 @@ func (e engine) Render(ctx context.Context, text string, dest io.Writer) error {
8187

8288
func (e engine) render(text string, dest io.Writer, data any) error {
8389
if _, err := e.template.Parse(text); err != nil {
84-
return err
90+
return fmt.Errorf("%w: %w", ErrRendering, err)
8591
}
8692

8793
tmp := &bytes.Buffer{}
8894
if err := e.template.Execute(tmp, data); err != nil {
89-
return err
95+
return fmt.Errorf("%w: %w", ErrRendering, err)
9096
}
9197

9298
b, err := e.encodeAndDecoder.EncodeAndDecode(tmp.Bytes())
@@ -96,7 +102,7 @@ func (e engine) render(text string, dest io.Writer, data any) error {
96102

97103
_, err = io.Copy(dest, bytes.NewReader(b))
98104
if err != nil {
99-
return err
105+
return fmt.Errorf("%w: %w", ErrRendering, err)
100106
}
101107

102108
return nil

pkg/errors/errors.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Copyright 2024 DWANGO Co., Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package errors
18+
19+
import (
20+
"errors"
21+
22+
"github.com/dwango/yashiro/internal/client/cache"
23+
"github.com/dwango/yashiro/pkg/engine"
24+
)
25+
26+
// IsCacheProcessingError returns true if the error is a cache processing error.
27+
func IsCacheProcessingError(err error) bool {
28+
return errors.Is(err, cache.ErrCacheProcessing)
29+
}
30+
31+
// IsRenderingError returns true if the error is a rendering error.
32+
func IsRenderingError(err error) bool {
33+
return errors.Is(err, engine.ErrRendering)
34+
}

pkg/errors/errors_test.go

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Copyright 2024 DWANGO Co., Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package errors
18+
19+
import (
20+
"errors"
21+
"fmt"
22+
"testing"
23+
24+
"github.com/dwango/yashiro/internal/client/cache"
25+
"github.com/dwango/yashiro/pkg/engine"
26+
)
27+
28+
func TestIsCacheProcessingError(t *testing.T) {
29+
type args struct {
30+
err error
31+
}
32+
tests := []struct {
33+
name string
34+
args args
35+
want bool
36+
}{
37+
{
38+
name: "true",
39+
args: args{
40+
err: fmt.Errorf("test: %w", cache.ErrCacheProcessing),
41+
},
42+
want: true,
43+
},
44+
{
45+
name: "false",
46+
args: args{
47+
err: fmt.Errorf("test: %w", errors.New("different error")),
48+
},
49+
want: false,
50+
},
51+
}
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
if got := IsCacheProcessingError(tt.args.err); got != tt.want {
55+
t.Errorf("IsCacheProcessingError() = %v, want %v", got, tt.want)
56+
}
57+
})
58+
}
59+
}
60+
61+
func TestIsRenderingError(t *testing.T) {
62+
type args struct {
63+
err error
64+
}
65+
tests := []struct {
66+
name string
67+
args args
68+
want bool
69+
}{
70+
{
71+
name: "true",
72+
args: args{
73+
err: fmt.Errorf("test: %w", engine.ErrRendering),
74+
},
75+
want: true,
76+
},
77+
{
78+
name: "false",
79+
args: args{
80+
err: fmt.Errorf("test: %w", errors.New("different error")),
81+
},
82+
want: false,
83+
},
84+
}
85+
for _, tt := range tests {
86+
t.Run(tt.name, func(t *testing.T) {
87+
if got := IsRenderingError(tt.args.err); got != tt.want {
88+
t.Errorf("IsRenderingError() = %v, want %v", got, tt.want)
89+
}
90+
})
91+
}
92+
}

0 commit comments

Comments
 (0)