Skip to content

Commit 39aa8c9

Browse files
authored
Add New...f constructors for all service errors (#221)
1 parent 546d09f commit 39aa8c9

22 files changed

+206
-4
lines changed

serviceerror/already_exists.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package serviceerror
22

33
import (
4+
"fmt"
5+
46
"google.golang.org/grpc/codes"
57
"google.golang.org/grpc/status"
68
)
@@ -13,13 +15,25 @@ type (
1315
}
1416
)
1517

16-
// NewAlreadyExist returns new AlreadyExists error.
18+
// Deprecated. Typo in the name. Use NewAlreadyExists instead.
1719
func NewAlreadyExist(message string) error {
20+
return NewAlreadyExists(message)
21+
}
22+
23+
// NewAlreadyExists returns new AlreadyExists error.
24+
func NewAlreadyExists(message string) error {
1825
return &AlreadyExists{
1926
Message: message,
2027
}
2128
}
2229

30+
// NewAlreadyExistsf returns new AlreadyExists error with formatted message.
31+
func NewAlreadyExistsf(format string, args ...any) error {
32+
return &AlreadyExists{
33+
Message: fmt.Sprintf(format, args...),
34+
}
35+
}
36+
2337
// Error returns string message.
2438
func (e *AlreadyExists) Error() string {
2539
return e.Message

serviceerror/canceled.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package serviceerror
22

33
import (
4+
"fmt"
5+
46
"google.golang.org/grpc/codes"
57
"google.golang.org/grpc/status"
68
)
@@ -20,6 +22,13 @@ func NewCanceled(message string) error {
2022
}
2123
}
2224

25+
// NewCanceledf returns new Canceled error with formatted message.
26+
func NewCanceledf(format string, args ...any) error {
27+
return &Canceled{
28+
Message: fmt.Sprintf(format, args...),
29+
}
30+
}
31+
2332
// Error returns string message.
2433
func (e *Canceled) Error() string {
2534
return e.Message

serviceerror/cancellation_already_requested.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package serviceerror
22

33
import (
4+
"fmt"
5+
46
"google.golang.org/grpc/codes"
57
"google.golang.org/grpc/status"
68

@@ -22,6 +24,13 @@ func NewCancellationAlreadyRequested(message string) error {
2224
}
2325
}
2426

27+
// NewCancellationAlreadyRequestedf returns new CancellationAlreadyRequested error with formatted message.
28+
func NewCancellationAlreadyRequestedf(format string, args ...any) error {
29+
return &CancellationAlreadyRequested{
30+
Message: fmt.Sprintf(format, args...),
31+
}
32+
}
33+
2534
// Error returns string message.
2635
func (e *CancellationAlreadyRequested) Error() string {
2736
return e.Message

serviceerror/client_version_not_supported.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ func NewClientVersionNotSupported(clientVersion, clientName, supportedVersions s
3030
}
3131
}
3232

33+
// NewClientVersionNotSupportedf returns new ClientVersionNotSupported error with formatted message.
34+
func NewClientVersionNotSupportedf(clientVersion, clientName, supportedVersions, format string, args ...any) error {
35+
return &ClientVersionNotSupported{
36+
Message: fmt.Sprintf(format, args...),
37+
ClientVersion: clientVersion,
38+
ClientName: clientName,
39+
SupportedVersions: supportedVersions,
40+
}
41+
}
42+
3343
// Error returns string message.
3444
func (e *ClientVersionNotSupported) Error() string {
3545
return e.Message

serviceerror/convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func FromStatus(st *status.Status) error {
176176
return st.Err()
177177
}
178178

179-
func extractErrorDetails(st *status.Status) interface{} {
179+
func extractErrorDetails(st *status.Status) any {
180180
details := st.Details()
181181
if len(details) > 0 {
182182
return details[0]

serviceerror/data_loss.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package serviceerror
22

33
import (
4+
"fmt"
5+
46
"google.golang.org/grpc/codes"
57
"google.golang.org/grpc/status"
68
)
@@ -20,6 +22,13 @@ func NewDataLoss(message string) error {
2022
}
2123
}
2224

25+
// NewDataLossf returns new DataLoss error with formatted message.
26+
func NewDataLossf(format string, args ...any) error {
27+
return &DataLoss{
28+
Message: fmt.Sprintf(format, args...),
29+
}
30+
}
31+
2332
// Error returns string message.
2433
func (e *DataLoss) Error() string {
2534
return e.Message

serviceerror/deadline_exceeded.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package serviceerror
22

33
import (
4+
"fmt"
5+
46
"google.golang.org/grpc/codes"
57
"google.golang.org/grpc/status"
68
)
@@ -20,6 +22,13 @@ func NewDeadlineExceeded(message string) error {
2022
}
2123
}
2224

25+
// NewDeadlineExceededf returns new DeadlineExceeded error with formatted message.
26+
func NewDeadlineExceededf(format string, args ...any) error {
27+
return &DeadlineExceeded{
28+
Message: fmt.Sprintf(format, args...),
29+
}
30+
}
31+
2332
// Error returns string message.
2433
func (e *DeadlineExceeded) Error() string {
2534
return e.Message

serviceerror/failed_precondition.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package serviceerror
22

33
import (
4+
"fmt"
5+
46
"google.golang.org/grpc/codes"
57
"google.golang.org/grpc/status"
68
)
@@ -20,6 +22,13 @@ func NewFailedPrecondition(message string) error {
2022
}
2123
}
2224

25+
// NewFailedPreconditionf returns new FailedPrecondition error with formatted message.
26+
func NewFailedPreconditionf(format string, args ...any) error {
27+
return &FailedPrecondition{
28+
Message: fmt.Sprintf(format, args...),
29+
}
30+
}
31+
2332
// Error returns string message.
2433
func (e *FailedPrecondition) Error() string {
2534
return e.Message

serviceerror/internal.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package serviceerror
22

33
import (
4+
"fmt"
5+
46
"google.golang.org/grpc/codes"
57
"google.golang.org/grpc/status"
68
)
@@ -20,6 +22,13 @@ func NewInternal(message string) error {
2022
}
2123
}
2224

25+
// NewInternalf returns new Internal error with formatted message.
26+
func NewInternalf(format string, args ...any) error {
27+
return &Internal{
28+
Message: fmt.Sprintf(format, args...),
29+
}
30+
}
31+
2332
// Error returns string message.
2433
func (e *Internal) Error() string {
2534
return e.Message

serviceerror/invalid_argument.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package serviceerror
22

33
import (
4+
"fmt"
5+
46
"google.golang.org/grpc/codes"
57
"google.golang.org/grpc/status"
68
)
@@ -20,6 +22,13 @@ func NewInvalidArgument(message string) error {
2022
}
2123
}
2224

25+
// NewInvalidArgumentf returns new InvalidArgument error with formatted message.
26+
func NewInvalidArgumentf(format string, args ...any) error {
27+
return &InvalidArgument{
28+
Message: fmt.Sprintf(format, args...),
29+
}
30+
}
31+
2332
// Error returns string message.
2433
func (e *InvalidArgument) Error() string {
2534
return e.Message

0 commit comments

Comments
 (0)