Skip to content

Commit cfc3874

Browse files
authored
Bump uma sdk, use standard errors (#172)
1 parent 3f5636f commit cfc3874

13 files changed

Lines changed: 434 additions & 409 deletions

File tree

examples/uma-server/currencies.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var SatsCurrency = umaprotocol.Currency{
1313
MinSendable: 1,
1414
MaxSendable: 100_000_000,
1515
},
16-
Decimals: 0,
16+
Decimals: 0,
1717
UmaMajorVersion: 1,
1818
}
1919

@@ -26,6 +26,6 @@ var UsdCurrency = umaprotocol.Currency{
2626
MinSendable: 1,
2727
MaxSendable: 1_000,
2828
},
29-
Decimals: 2,
29+
Decimals: 2,
3030
UmaMajorVersion: 1,
3131
}

examples/uma-server/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/gin-gonic/gin v1.10.0
1111
github.com/google/uuid v1.6.0
1212
github.com/lightsparkdev/go-sdk v0.15.1
13-
github.com/uma-universal-money-address/uma-go-sdk v1.3.1
13+
github.com/uma-universal-money-address/uma-go-sdk v1.5.0
1414
)
1515

1616
require (

examples/uma-server/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,8 @@ github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGr
736736
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
737737
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
738738
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
739-
github.com/uma-universal-money-address/uma-go-sdk v1.3.1 h1:4VN+jzzr3d0GMyrH5kGG1CUSRvVFLWMrjpGWtFN4IRw=
740-
github.com/uma-universal-money-address/uma-go-sdk v1.3.1/go.mod h1:dXNOwmUxZzJ6szhn8Cj3ajwnuUrr27rYIIRntWbynaU=
739+
github.com/uma-universal-money-address/uma-go-sdk v1.5.0 h1:zwoDJxQrmAU7hTajrkMdnfVK4fDUxe8sKcz96CeUkpw=
740+
github.com/uma-universal-money-address/uma-go-sdk v1.5.0/go.mod h1:dXNOwmUxZzJ6szhn8Cj3ajwnuUrr27rYIIRntWbynaU=
741741
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
742742
github.com/urfave/cli/v2 v2.10.2/go.mod h1:f8iq5LtQ/bLxafbdBSLPPNsgaW0l/2fYYEHhAyPlwvo=
743743
github.com/urfave/cli/v2 v2.24.1/go.mod h1:GHupkWPMM0M/sj1a2b4wUrWBPzazNrIjouW6fmdJLxc=

examples/uma-server/lightspark_helpers.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package main
22

33
import (
4-
"errors"
54
"github.com/lightsparkdev/go-sdk/objects"
65
"github.com/lightsparkdev/go-sdk/services"
6+
"github.com/uma-universal-money-address/uma-go-sdk/uma/errors"
7+
"github.com/uma-universal-money-address/uma-go-sdk/uma/generated"
78
)
89

910
func GetNode(client *services.LightsparkClient, nodeId string) (*objects.LightsparkNode, error) {
@@ -14,7 +15,10 @@ func GetNode(client *services.LightsparkClient, nodeId string) (*objects.Lightsp
1415

1516
castNode, didCast := (*entity).(objects.LightsparkNode)
1617
if !didCast {
17-
return nil, errors.New("failed to cast entity to LightsparkNode")
18+
return nil, &errors.UmaError{
19+
Reason: "failed to cast entity to LightsparkNode",
20+
ErrorCode: generated.InternalError,
21+
}
1822
}
1923

2024
return &castNode, nil

examples/uma-server/server.go

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package main
22

33
import (
4+
"encoding/json"
45
"fmt"
5-
"github.com/gin-contrib/sessions"
6-
"github.com/gin-contrib/sessions/cookie"
76
"log"
87
"net/http"
98
"os"
109
"strings"
1110
"time"
1211

12+
"github.com/gin-contrib/sessions"
13+
"github.com/gin-contrib/sessions/cookie"
14+
1315
"github.com/gin-gonic/gin"
1416
"github.com/uma-universal-money-address/uma-go-sdk/uma"
17+
"github.com/uma-universal-money-address/uma-go-sdk/uma/errors"
18+
"github.com/uma-universal-money-address/uma-go-sdk/uma/generated"
1519
umautils "github.com/uma-universal-money-address/uma-go-sdk/uma/utils"
1620
)
1721

@@ -41,9 +45,9 @@ func main() {
4145
oneDayAgo := time.Now().AddDate(0, 0, -1)
4246
userService := NewUserServiceFromEnv(config)
4347

48+
engine.Use(UmaErrorHandler())
4449
engine.Use(gin.Logger())
4550
engine.Use(gin.Recovery())
46-
engine.Use(gin.ErrorLogger())
4751

4852
// Require authentication for all API routes.
4953
engine.Use(func(c *gin.Context) {
@@ -53,7 +57,7 @@ func main() {
5357
}
5458
user, err := userService.GetUserFromContext(c)
5559
if err != nil {
56-
c.AbortWithStatus(http.StatusUnauthorized)
60+
c.Error(err)
5761
return
5862
}
5963
c.Set("user_id", user.ID)
@@ -183,3 +187,45 @@ func main() {
183187
}
184188
engine.Run(":" + port)
185189
}
190+
191+
func UmaErrorHandler() gin.HandlerFunc {
192+
return func(c *gin.Context) {
193+
c.Next()
194+
for _, err := range c.Errors {
195+
switch e := err.Err.(type) {
196+
case *errors.UmaError:
197+
c.AbortWithStatusJSON(e.ToHttpStatusCode(), umaErrorToMap(e))
198+
return
199+
default:
200+
internalError := &errors.UmaError{
201+
Reason: c.Errors.Last().Error(),
202+
ErrorCode: generated.InternalError,
203+
}
204+
c.AbortWithStatusJSON(internalError.ToHttpStatusCode(), umaErrorToMap(internalError))
205+
return
206+
}
207+
}
208+
}
209+
}
210+
211+
func umaErrorToMap(umaError *errors.UmaError) map[string]interface{} {
212+
jsonStr, marshalErr := umaError.ToJSON()
213+
if marshalErr != nil {
214+
return map[string]interface{}{
215+
"status": "ERROR",
216+
"reason": "Failed to serialize UMA error",
217+
"code": generated.InternalError.Code,
218+
}
219+
} else {
220+
var jsonMap map[string]interface{}
221+
if err := json.Unmarshal([]byte(jsonStr), &jsonMap); err != nil {
222+
return map[string]interface{}{
223+
"status": "ERROR",
224+
"reason": "Failed to parse error JSON",
225+
"code": generated.InternalError.Code,
226+
}
227+
} else {
228+
return jsonMap
229+
}
230+
}
231+
}

examples/uma-server/user_service.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import (
44
"crypto/sha256"
55
"encoding/base64"
66
"fmt"
7+
"strings"
8+
79
"github.com/gin-contrib/sessions"
810
"github.com/gin-gonic/gin"
9-
"strings"
11+
"github.com/uma-universal-money-address/uma-go-sdk/uma/errors"
12+
"github.com/uma-universal-money-address/uma-go-sdk/uma/generated"
1013
)
1114

1215
// User represents a user in the system.
@@ -52,14 +55,20 @@ func NewUserServiceFromEnv(config UmaConfig) *UserServiceFromEnv {
5255

5356
func (u *UserServiceFromEnv) GetUser(id string) (*User, error) {
5457
if u.user == nil || u.user.ID != id {
55-
return nil, fmt.Errorf("user not found")
58+
return nil, &errors.UmaError{
59+
Reason: "User not found",
60+
ErrorCode: generated.UserNotFound,
61+
}
5662
}
5763
return u.user, nil
5864
}
5965

6066
func (u *UserServiceFromEnv) GetUserFromContext(context *gin.Context) (*User, error) {
6167
if u.user == nil {
62-
return nil, fmt.Errorf("user not found")
68+
return nil, &errors.UmaError{
69+
Reason: "User not found",
70+
ErrorCode: generated.UserNotFound,
71+
}
6372
}
6473

6574
// Get from session cookie:
@@ -75,11 +84,17 @@ func (u *UserServiceFromEnv) GetUserFromContext(context *gin.Context) (*User, er
7584
encodedCredentials := strings.TrimPrefix(authHeader, basicToken)
7685
decodedCredentials, err := base64.StdEncoding.DecodeString(encodedCredentials)
7786
if err != nil {
78-
return nil, fmt.Errorf("error decoding credentials: %v", err)
87+
return nil, &errors.UmaError{
88+
Reason: fmt.Sprintf("error decoding credentials: %v", err),
89+
ErrorCode: generated.Forbidden,
90+
}
7991
}
8092
credentials := strings.Split(string(decodedCredentials), ":")
8193
if len(credentials) != 2 {
82-
return nil, fmt.Errorf("invalid credentials")
94+
return nil, &errors.UmaError{
95+
Reason: "invalid credentials",
96+
ErrorCode: generated.Forbidden,
97+
}
8398
}
8499
password := credentials[1]
85100
hashedPassword := hashString(password)
@@ -88,12 +103,18 @@ func (u *UserServiceFromEnv) GetUserFromContext(context *gin.Context) (*User, er
88103
}
89104
}
90105

91-
return nil, fmt.Errorf("user not found")
106+
return nil, &errors.UmaError{
107+
Reason: "user not found",
108+
ErrorCode: generated.UserNotFound,
109+
}
92110
}
93111

94112
func (u *UserServiceFromEnv) GetUserByUmaAddress(umaAddress string, config UmaConfig, context *gin.Context) (*User, error) {
95113
if u.user == nil || u.user.GetUmaAddress(&config, context) != umaAddress {
96-
return nil, fmt.Errorf("user not found")
114+
return nil, &errors.UmaError{
115+
Reason: "user not found",
116+
ErrorCode: generated.UserNotFound,
117+
}
97118
}
98119
return u.user, nil
99120
}

examples/uma-server/validators.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package main
22

33
import (
4-
"errors"
4+
"github.com/uma-universal-money-address/uma-go-sdk/uma/errors"
5+
"github.com/uma-universal-money-address/uma-go-sdk/uma/generated"
56
umautils "github.com/uma-universal-money-address/uma-go-sdk/uma/utils"
67
"regexp"
78
"strings"
@@ -10,7 +11,10 @@ import (
1011
func ValidateUmaAddress(address string) error {
1112
addressParts := strings.Split(address, "@")
1213
if len(addressParts) != 2 {
13-
return errors.New("invalid receiver address")
14+
return &errors.UmaError{
15+
Reason: "invalid receiver address",
16+
ErrorCode: generated.InvalidInput,
17+
}
1418
}
1519
receiverId := addressParts[0]
1620
receiverVasp := addressParts[1]
@@ -28,7 +32,10 @@ func ValidateUmaAddress(address string) error {
2832
func ValidateUserName(userName string) error {
2933
userNameRegex := regexp.MustCompile(`^\$?[a-zA-Z0-9-._+]+$`)
3034
if !userNameRegex.MatchString(userName) {
31-
return errors.New("invalid UMA user name")
35+
return &errors.UmaError{
36+
Reason: "invalid UMA user name",
37+
ErrorCode: generated.InvalidInput,
38+
}
3239
}
3340
return nil
3441
}
@@ -39,7 +46,10 @@ func ValidateDomain(domain string) error {
3946
localHostWithPortRegex := regexp.MustCompile(`^localhost(:[0-9]+)?$`)
4047
domainRegex := regexp.MustCompile(`^([a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62}){1}(\.[a-zA-Z0-9_]{1}[a-zA-Z0-9_-]{0,62})*[._]?$`)
4148
if !domainRegex.MatchString(domain) && !localHostWithPortRegex.MatchString(domain) && !isLocalDomain {
42-
return errors.New("invalid VASP domain")
49+
return &errors.UmaError{
50+
Reason: "invalid VASP domain",
51+
ErrorCode: generated.InvalidInput,
52+
}
4353
}
4454
return nil
4555
}

0 commit comments

Comments
 (0)