Skip to content

Commit

Permalink
Fix error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
krishna2803 committed Jan 1, 2025
1 parent 4f748bd commit 95cca57
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
14 changes: 6 additions & 8 deletions api/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package api

import (
"net/http"
"strconv"
"strings"

"github.com/gin-gonic/gin"

Expand Down Expand Up @@ -148,7 +146,7 @@ func HandleUpdateProfile(c *gin.Context) {
if err != nil {
log.ErrorLogger("Kratos post settings update profile flow failed", err)

errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Kratos post settings update profile flow failed",
Expand Down Expand Up @@ -205,7 +203,7 @@ func HandleChangePassword(c *gin.Context) {
if err != nil {
log.ErrorLogger("Kratos post settings change password flow failed", err)

errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Kratos post settings change password flow failed",
Expand Down Expand Up @@ -235,7 +233,7 @@ func HandleChangePassword(c *gin.Context) {
if err != nil {
log.ErrorLogger("Kratos post settings update profile flow failed", err)

errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Kratos post settings update profile flow failed",
Expand Down Expand Up @@ -291,10 +289,10 @@ func HandleToggleTOTP(c *gin.Context) {
if err != nil {
log.ErrorLogger("Kratos post settings toggle totp flow failed", err)

errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Kratos post settings toggle totp flow failed",
"message": msg,
})
return
}
Expand Down Expand Up @@ -323,7 +321,7 @@ func HandleToggleTOTP(c *gin.Context) {
if err != nil {
log.ErrorLogger("Kratos post settings update profile flow failed", err)

errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Kratos post settings update profile flow failed",
Expand Down
4 changes: 2 additions & 2 deletions pkg/wrapper/kratos/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func SubmitLoginFlowWrapper(cookie string, flowID string, csrfToken string, pass
responseCookies := r.Header["Set-Cookie"]

if err != nil {
msg := helper.ExtractErrorMessage(r)
if responseCookies == nil {
msg := helper.ExtractErrorMessage(r)
return *client.NewSessionWithDefaults(), "", msg, err
}
return *client.NewSessionWithDefaults(), responseCookies[1], "", err
return *client.NewSessionWithDefaults(), responseCookies[1], msg, err
}

return resp.Session, responseCookies[1], "", nil
Expand Down
28 changes: 19 additions & 9 deletions pkg/wrapper/kratos/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
client "github.com/ory/client-go"

"github.com/sdslabs/nymeria/config"
"github.com/sdslabs/nymeria/helper"
)

func InitializeSettingsFlowWrapper(session_cookie string) (client.SettingsFlow, string, error) {
Expand Down Expand Up @@ -41,12 +42,15 @@ func SubmitSettingsFlowPasswordMethod(flow_cookie string, session_cookie string,
_, r, err := apiClient.FrontendAPI.UpdateSettingsFlow(context.Background()).Flow(flowID).Cookie(cookie).UpdateSettingsFlowBody(submitFlowBody).Execute()

if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.SubmitSelfServiceVerificationFlow``: %v\n", err)
msg := helper.ExtractErrorMessage(r)
fmt.Fprintf(os.Stderr, "Error when calling `SubmitSettingsFlowPasswordMethod`: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
return "", err
return msg, err
}

return "Password Changed", nil
msg := helper.ExtractSuccessMessage(r)

return msg + " Password Changed", nil
}

func SubmitSettingsFlowProfileMethod(flow_cookie string, session_cookie string, flowID string, csrfToken string, traits map[string]interface{}) (string, error) {
Expand All @@ -62,12 +66,15 @@ func SubmitSettingsFlowProfileMethod(flow_cookie string, session_cookie string,
_, r, err := apiClient.FrontendAPI.UpdateSettingsFlow(context.Background()).Flow(flowID).Cookie(cookie).UpdateSettingsFlowBody(submitFlowBody).Execute()

if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.SubmitSelfServiceVerificationFlow``: %v\n", err)
msg := helper.ExtractErrorMessage(r)
fmt.Fprintf(os.Stderr, "Error when calling `SubmitSettingsFlowProfileMethod`: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
return "", err
return msg, err
}

return "Profile Updated", nil
msg := helper.ExtractSuccessMessage(r)

return msg + " Profile Updated", nil
}

func SubmitSettingsFlowTOTPMethod(flow_cookie string, session_cookie string, flowID string, csrfToken string, TOTPcode string, TOTPUnlink bool) (string, error) {
Expand All @@ -85,10 +92,13 @@ func SubmitSettingsFlowTOTPMethod(flow_cookie string, session_cookie string, flo
_, r, err := apiClient.FrontendAPI.UpdateSettingsFlow(context.Background()).Flow(flowID).Cookie(cookie).UpdateSettingsFlowBody(submitFlowBody).Execute()

if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.SubmitSelfServiceVerificationFlow``: %v\n", err)
msg := helper.ExtractErrorMessage(r)
fmt.Fprintf(os.Stderr, "Error when calling `SubmitSettingsFlowTOTPMethod`: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
return "", err
return msg, err
}

return "Totp Toggled", nil
msg := helper.ExtractSuccessMessage(r)

return msg + " Totp Toggled", nil
}

0 comments on commit 95cca57

Please sign in to comment.