-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
148ac37
commit 1bdbe91
Showing
6 changed files
with
131 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright 2024 The Yorkie Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Package metaerrors provides a way to attach metadata to errors. | ||
package metaerrors | ||
|
||
import "strings" | ||
|
||
// MetaError is an error that can have metadata attached to it. This can be used | ||
// to send additional information to the SDK or to the user. | ||
type MetaError struct { | ||
// Err is the underlying error. | ||
Err error | ||
|
||
// Metadata is a map of additional information that can be attached to the | ||
// error. | ||
Metadata map[string]string | ||
} | ||
|
||
// New returns a new MetaError with the given error and metadata. | ||
func New(err error, metadata map[string]string) *MetaError { | ||
return &MetaError{ | ||
Err: err, | ||
Metadata: metadata, | ||
} | ||
} | ||
|
||
// Error returns the error message. | ||
func (e MetaError) Error() string { | ||
if len(e.Metadata) == 0 { | ||
return e.Err.Error() | ||
} | ||
|
||
sb := strings.Builder{} | ||
|
||
for key, val := range e.Metadata { | ||
if sb.Len() > 0 { | ||
sb.WriteString(",") | ||
} | ||
sb.WriteString(key) | ||
sb.WriteString("=") | ||
sb.WriteString(val) | ||
} | ||
|
||
return e.Err.Error() + " [" + sb.String() + "]" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2024 The Yorkie Authors. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package metaerrors_test | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/yorkie-team/yorkie/internal/metaerrors" | ||
) | ||
|
||
func TestMetaError(t *testing.T) { | ||
t.Run("test meta error", func(t *testing.T) { | ||
err := errors.New("error message") | ||
metaErr := metaerrors.New(err, map[string]string{"key": "value"}) | ||
assert.Equal(t, "error message [key=value]", metaErr.Error()) | ||
|
||
err = errors.New("error message") | ||
metaErr = metaerrors.New(err, map[string]string{"key1": "value1", "key2": "value2"}) | ||
assert.Equal(t, "error message [key1=value1,key2=value2]", metaErr.Error()) | ||
}) | ||
|
||
t.Run("test meta error without metadata", func(t *testing.T) { | ||
err := errors.New("error message") | ||
metaErr := metaerrors.New(err, nil) | ||
assert.Equal(t, "error message", metaErr.Error()) | ||
}) | ||
|
||
t.Run("test meta error with wrapped error", func(t *testing.T) { | ||
err := fmt.Errorf("wrapped error: %w", errors.New("error message")) | ||
metaErr := metaerrors.New(err, map[string]string{"key": "value"}) | ||
assert.Equal(t, "wrapped error: error message [key=value]", metaErr.Error()) | ||
|
||
metaErr = metaerrors.New(errors.New("error message"), map[string]string{"key": "value"}) | ||
assert.Equal(t, "error message [key=value]", metaErr.Error()) | ||
|
||
wrappedErr := fmt.Errorf("wrapped error: %w", metaErr) | ||
assert.Equal(t, "wrapped error: error message [key=value]", wrappedErr.Error()) | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters