Skip to content

Commit

Permalink
Adds error message extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
Aryan51203 committed Dec 23, 2024
1 parent b205949 commit e7316de
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 97 deletions.
23 changes: 11 additions & 12 deletions api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import (
"fmt"
"net/http"
"os"
"strconv"
"strings"

"github.com/gin-gonic/gin"

"github.com/sdslabs/nymeria/helper"
"github.com/sdslabs/nymeria/log"
"github.com/sdslabs/nymeria/pkg/middleware"
"github.com/sdslabs/nymeria/pkg/wrapper/kratos/admin"
Expand All @@ -24,7 +23,7 @@ func HandleCreateIdentityFlow(c *gin.Context) {
if err != nil {
log.ErrorLogger("Unable to process JSON body", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to process JSON body",
Expand Down Expand Up @@ -65,7 +64,7 @@ func HandleGetIdentityFlow(c *gin.Context) {
if err != nil {
log.ErrorLogger("Unable to convert map to json", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to convert map to json",
Expand All @@ -80,7 +79,7 @@ func HandleGetIdentityFlow(c *gin.Context) {
if err != nil {
log.ErrorLogger("Unable to convert JSON to map", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to convert JSON to map",
Expand All @@ -103,7 +102,7 @@ func HandleDeleteIdentityFlow(c *gin.Context) {
if err != nil {
log.ErrorLogger("Unable to process JSON body", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to process JSON body",
Expand All @@ -114,7 +113,7 @@ func HandleDeleteIdentityFlow(c *gin.Context) {
session, err := middleware.GetSession(c)
if err != nil {
log.ErrorLogger("Unable to get session", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to get session",
Expand Down Expand Up @@ -170,7 +169,7 @@ func HandleBanIdentity(c *gin.Context) {
if err != nil {
log.ErrorLogger("Unable to process JSON body", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to process JSON body",
Expand All @@ -181,7 +180,7 @@ func HandleBanIdentity(c *gin.Context) {
session, err := middleware.GetSession(c)
if err != nil {
log.ErrorLogger("Unable to get session", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to get session",
Expand Down Expand Up @@ -232,7 +231,7 @@ func HandleRemoveBanIdentity(c *gin.Context) {
if err != nil {
log.ErrorLogger("Unable to process JSON body", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to process JSON body",
Expand Down Expand Up @@ -273,7 +272,7 @@ func HandleRoleSwitch(c *gin.Context) {
if err != nil {
log.ErrorLogger("Unable to process JSON body", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to process JSON body",
Expand All @@ -284,7 +283,7 @@ func HandleRoleSwitch(c *gin.Context) {
session, err := middleware.GetSession(c)
if err != nil {
log.ErrorLogger("Unable to get session", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to get session",
Expand Down
10 changes: 5 additions & 5 deletions api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func HandleGetApplication(c *gin.Context) {
if err != nil {
log.ErrorLogger("Unable to get application data", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": strings.Split(err.Error(), " ")[1],
"message": "Unable to get application data",
Expand All @@ -39,7 +39,7 @@ func HandlePostApplication(c *gin.Context) {
if err != nil {
log.ErrorLogger("Unable to process json body", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": strings.Split(err.Error(), " ")[1],
"message": "Unable to process json body",
Expand Down Expand Up @@ -73,7 +73,7 @@ func HandlePutApplication(c *gin.Context) {
if err != nil {
log.ErrorLogger("Unable to process json body", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": strings.Split(err.Error(), " ")[1],
"message": "Unable to process json body",
Expand Down Expand Up @@ -107,7 +107,7 @@ func HandleDeleteApplication(c *gin.Context) {
if err != nil {
log.ErrorLogger("Unable to process json body", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": strings.Split(err.Error(), " ")[1],
"message": "Unable to process json body",
Expand Down Expand Up @@ -141,7 +141,7 @@ func HandleUpdateClientSecret(c *gin.Context) {
if err != nil {
log.ErrorLogger("Unable to process json body", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": strings.Split(err.Error(), " ")[1],
"message": "Unable to process json body",
Expand Down
12 changes: 7 additions & 5 deletions api/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/gin-gonic/gin"

"github.com/sdslabs/nymeria/config"
"github.com/sdslabs/nymeria/helper"
"github.com/sdslabs/nymeria/log"
"github.com/sdslabs/nymeria/pkg/wrapper/kratos/login"
)
Expand All @@ -19,7 +20,8 @@ func HandleGetLoginFlow(c *gin.Context) {
if err != nil {
log.ErrorLogger("Initialize Login Failed", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)

c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Initialize Login Failed",
Expand All @@ -42,7 +44,7 @@ func HandlePostLoginFlow(c *gin.Context) {
if err != nil {
log.ErrorLogger("Unable to process json body", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to process json body",
Expand All @@ -55,23 +57,23 @@ func HandlePostLoginFlow(c *gin.Context) {
if err != nil {
log.ErrorLogger("Cookie not found", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Cookie not found",
})
return
}

identity, session, err := login.SubmitLoginFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Password, t.Identifier) // _ is USERID
identity, session, errMsg, err := login.SubmitLoginFlowWrapper(cookie, t.FlowID, t.CsrfToken, t.Password, t.Identifier) // _ is USERID

if session == "" {
log.ErrorLogger("Post login flow failed", err)

errCode, _ := strconv.Atoi((strings.Split(err.Error(), " "))[0])
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Kratos post login flow failed",
"message": errMsg,
})
return
}
Expand Down
13 changes: 6 additions & 7 deletions api/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ package api

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

"github.com/gin-gonic/gin"

"github.com/sdslabs/nymeria/config"
"github.com/sdslabs/nymeria/helper"
"github.com/sdslabs/nymeria/log"
"github.com/sdslabs/nymeria/pkg/wrapper/kratos/logout"
)
Expand All @@ -17,7 +16,7 @@ func HandleGetLogoutFlow(c *gin.Context) {

if err != nil {
log.ErrorLogger("Session cookie not found", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Session cookie not found",
Expand All @@ -29,7 +28,7 @@ func HandleGetLogoutFlow(c *gin.Context) {

if err != nil {
log.ErrorLogger("Kratos get logout 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 get logout flow failed",
Expand All @@ -49,7 +48,7 @@ func HandlePostLogoutFlow(c *gin.Context) {

if err != nil {
log.ErrorLogger("Unable to process json body", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to process json body",
Expand All @@ -61,7 +60,7 @@ func HandlePostLogoutFlow(c *gin.Context) {

if err != nil {
log.ErrorLogger("Session cookie not found", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Session cookie not found",
Expand All @@ -72,7 +71,7 @@ func HandlePostLogoutFlow(c *gin.Context) {

if err != nil {
log.ErrorLogger("Kratos get logout 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 get logout flow failed",
Expand Down
6 changes: 3 additions & 3 deletions api/mfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package api

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

"github.com/gin-gonic/gin"

"github.com/sdslabs/nymeria/config"
"github.com/sdslabs/nymeria/helper"
"github.com/sdslabs/nymeria/log"
"github.com/sdslabs/nymeria/pkg/wrapper/kratos/login"
)
Expand All @@ -19,7 +19,7 @@ func HandleGetMFAFlow(c *gin.Context) {
if err != nil {
log.ErrorLogger("Session Cookie not found", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Cookie not found",
Expand Down Expand Up @@ -72,7 +72,7 @@ func HandlePostMFAFlow(c *gin.Context) {
if err != nil {
log.ErrorLogger("Session Cookie not found", err)

errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Cookie not found",
Expand Down
7 changes: 3 additions & 4 deletions api/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package api

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

"github.com/gin-gonic/gin"

"github.com/sdslabs/nymeria/helper"
"github.com/sdslabs/nymeria/log"
"github.com/sdslabs/nymeria/pkg/middleware"
)
Expand All @@ -15,7 +14,7 @@ func HandlePostProfile(c *gin.Context) {
session, err := middleware.GetSession(c)
if err != nil {
log.ErrorLogger("Unable to get session", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to get session",
Expand All @@ -33,7 +32,7 @@ func HandleGetVerifiedStatus(c *gin.Context) {
session, err := middleware.GetSession(c)
if err != nil {
log.ErrorLogger("Unable to get session", err)
errCode, _ := strconv.Atoi(strings.Split(err.Error(), " ")[0])
errCode := helper.ExtractErrorCode(err)
c.JSON(errCode, gin.H{
"error": err.Error(),
"message": "Unable to get session",
Expand Down
Loading

0 comments on commit e7316de

Please sign in to comment.