Skip to content

Commit 108488c

Browse files
committed
chore: fix application error messages.
Signed-off-by: krishna2803 <[email protected]>
1 parent aa877b8 commit 108488c

File tree

3 files changed

+91
-34
lines changed

3 files changed

+91
-34
lines changed

api/application.go

+19-16
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ func HandleGetApplication(c *gin.Context) {
1616

1717
if err != nil {
1818
log.ErrorLogger("Unable to get application data", err)
19-
2019
errCode := helper.ExtractErrorCode(err)
20+
errStr := helper.ExtractErrorString(err)
2121
c.JSON(errCode, gin.H{
22-
"error": strings.Split(err.Error(), " ")[1],
23-
"message": "Unable to get application data",
22+
"error": http.StatusText(errCode),
23+
"message": "Unable to get application data: " + errStr,
2424
})
2525
return
2626
}
@@ -36,11 +36,11 @@ func HandlePostApplication(c *gin.Context) {
3636
err := c.BindJSON(&body)
3737

3838
if err != nil {
39-
log.ErrorLogger("Unable to process json body", err)
40-
4139
errCode := helper.ExtractErrorCode(err)
40+
41+
log.ErrorLogger("Couldn't create application.", err)
4242
c.JSON(errCode, gin.H{
43-
"error": strings.Split(err.Error(), " ")[1],
43+
"error": http.StatusText(errCode),
4444
"message": "Unable to process json body",
4545
})
4646
return
@@ -52,9 +52,10 @@ func HandlePostApplication(c *gin.Context) {
5252
log.ErrorLogger("Create application failed", err)
5353

5454
errCode := helper.ExtractErrorCode(err)
55+
errStr := helper.ExtractErrorString(err)
5556
c.JSON(errCode, gin.H{
56-
"error": strings.Split(err.Error(), " ")[1],
57-
"message": "Create application failed",
57+
"error": http.StatusText(errCode),
58+
"message": "Create application failed: " + errStr,
5859
})
5960
return
6061
}
@@ -74,7 +75,7 @@ func HandlePutApplication(c *gin.Context) {
7475

7576
errCode := helper.ExtractErrorCode(err)
7677
c.JSON(errCode, gin.H{
77-
"error": strings.Split(err.Error(), " ")[1],
78+
"error": http.StatusText(errCode),
7879
"message": "Unable to process json body",
7980
})
8081
return
@@ -86,9 +87,10 @@ func HandlePutApplication(c *gin.Context) {
8687
log.ErrorLogger("Update application failed", err)
8788

8889
errCode := helper.ExtractErrorCode(err)
90+
errStr := helper.ExtractErrorString(err)
8991
c.JSON(errCode, gin.H{
90-
"error": strings.Split(err.Error(), " ")[1],
91-
"message": "Update application failed",
92+
"error": http.StatusText(errCode),
93+
"message": "Update application failed: " + errStr,
9294
})
9395
return
9496
}
@@ -108,7 +110,7 @@ func HandleDeleteApplication(c *gin.Context) {
108110

109111
errCode := helper.ExtractErrorCode(err)
110112
c.JSON(errCode, gin.H{
111-
"error": strings.Split(err.Error(), " ")[1],
113+
"error": http.StatusText(errCode),
112114
"message": "Unable to process json body",
113115
})
114116
return
@@ -120,15 +122,16 @@ func HandleDeleteApplication(c *gin.Context) {
120122
log.ErrorLogger("Delete application failed", err)
121123

122124
errCode := helper.ExtractErrorCode(err)
125+
errStr := helper.ExtractErrorString(err)
123126
c.JSON(errCode, gin.H{
124-
"error": strings.Split(err.Error(), " ")[1],
125-
"message": "Delete application failed",
127+
"error": http.StatusText(errCode),
128+
"message": "Delete application failed: " + errStr,
126129
})
127130
return
128131
}
129132

130133
c.JSON(http.StatusOK, gin.H{
131-
"status": "application deleted",
134+
"message": "application deleted",
132135
})
133136

134137
}
@@ -162,7 +165,7 @@ func HandleUpdateClientSecret(c *gin.Context) {
162165
}
163166

164167
c.JSON(http.StatusOK, gin.H{
165-
"status": "Client Secret updated successfully",
168+
"message": "Client Secret updated successfully",
166169
})
167170

168171
}

helper/extracter.go

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ func ExtractErrorCode(Error error) int {
2424
}
2525
return errCode
2626
}
27+
func ExtractErrorString(Error error) string {
28+
if Error == nil {
29+
return ""
30+
}
31+
32+
parts := strings.SplitN(Error.Error(), " ", 2)
33+
if len(parts) < 2 {
34+
return Error.Error()
35+
}
36+
37+
return strings.TrimSpace(parts[1])
38+
}
2739

2840
func ExtractSuccessMessage(r *http.Response) string {
2941

pkg/db/application.go

+60-18
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,100 @@
11
package db
22

3-
import "github.com/sdslabs/nymeria/helper"
3+
import (
4+
"errors"
5+
"fmt"
46

5-
func CreateApplication(name string, redirectURL string, allowedDomains string, organization string, clientKey string, clientSecret string) error {
6-
sqlStatement := `INSERT INTO application (name, redirect_url, allowed_domains, organization, created_at, client_key, client_secret) VALUES ($1, $2, $3, $4, now(), $5,$6);`
7-
db, err := Connection()
7+
"github.com/sdslabs/nymeria/helper"
8+
)
9+
10+
func CreateApplication(name, redirectURL, allowedDomains, organization, clientKey, clientSecret string) error {
11+
if name == "" {
12+
return errors.New("400 application name is required")
13+
}
14+
if redirectURL == "" {
15+
return errors.New("400 redirect URL is required")
16+
}
17+
if allowedDomains == "" {
18+
return errors.New("400 allowed domains is required")
19+
}
20+
if organization == "" {
21+
return errors.New("400 organization is required")
22+
}
23+
if clientKey == "" {
24+
return errors.New("400 client key is required")
25+
}
26+
if clientSecret == "" {
27+
return errors.New("400 client secret is required")
28+
}
29+
30+
sqlStatement := `INSERT INTO application (name, redirect_url, allowed_domains, organization, created_at, client_key, client_secret)
31+
VALUES ($1, $2, $3, $4, now(), $5, $6);`
832

33+
db, err := Connection()
934
if err != nil {
10-
return err
35+
return fmt.Errorf("500 database connection error: %w", err)
1136
}
1237
defer db.Close()
1338

1439
_, err = db.Exec(sqlStatement, name, redirectURL, allowedDomains, organization, clientKey, clientSecret)
15-
1640
if err != nil {
17-
return err
41+
return fmt.Errorf("500 failed to create application: %w", err)
1842
}
1943

2044
return nil
21-
2245
}
2346

24-
func UpdateApplication(id int, name string, redirectURL string, allowedDomains string, organization string) error {
47+
func UpdateApplication(id int, name, redirectURL, allowedDomains, organization string) error {
48+
if name == "" {
49+
return errors.New("400 application name is required")
50+
}
51+
if redirectURL == "" {
52+
return errors.New("400 redirect URL is required")
53+
}
54+
if allowedDomains == "" {
55+
return errors.New("400 allowed domains is required")
56+
}
57+
if organization == "" {
58+
return errors.New("400 organization is required")
59+
}
60+
2561
sqlStatement := `UPDATE application SET name=$1, redirect_url=$2, allowed_domains=$3, organization=$4 WHERE id=$5;`
26-
db, err := Connection()
2762

63+
db, err := Connection()
2864
if err != nil {
29-
return err
65+
return fmt.Errorf("500 database connection error: %w", err)
3066
}
3167
defer db.Close()
3268

3369
_, err = db.Exec(sqlStatement, name, redirectURL, allowedDomains, organization, id)
34-
3570
if err != nil {
36-
return err
71+
return fmt.Errorf("500 failed to update application: %w", err)
3772
}
3873

3974
return nil
40-
4175
}
4276

4377
func DeleteApplication(id int) error {
4478
sqlStatement := `DELETE FROM application WHERE id=$1;`
45-
db, err := Connection()
4679

80+
db, err := Connection()
4781
if err != nil {
48-
return err
82+
return fmt.Errorf("500 database connection error: %w", err)
4983
}
5084
defer db.Close()
5185

52-
_, err = db.Exec(sqlStatement, id)
86+
result, err := db.Exec(sqlStatement, id)
87+
if err != nil {
88+
return fmt.Errorf("500 failed to delete application: %w", err)
89+
}
5390

91+
rowsAffected, err := result.RowsAffected()
5492
if err != nil {
55-
return err
93+
return fmt.Errorf("500 error checking affected rows: %w", err)
94+
}
95+
96+
if rowsAffected == 0 {
97+
return errors.New("404 application not found")
5698
}
5799

58100
return nil

0 commit comments

Comments
 (0)