feat:#150 rewrite error handling for routes

This commit is contained in:
Elmar Kresse
2024-01-23 18:16:12 +01:00
parent 1582154d5f
commit 0198e74652
3 changed files with 66 additions and 60 deletions

View File

@@ -26,7 +26,7 @@ func AddRoutes(app *pocketbase.PocketBase) {
savedEvents, err := v2.ParseEventsFromRemote(app)
if err != nil {
slog.Error("Failed to parse events from remote: %v", err)
return c.JSON(http.StatusInternalServerError, "Failed to parse events from remote")
return c.JSON(http.StatusBadRequest, "Failed to parse events from remote")
} else {
return c.JSON(http.StatusOK, savedEvents)
}
@@ -49,7 +49,7 @@ func AddRoutes(app *pocketbase.PocketBase) {
Handler: func(c echo.Context) error {
groups, err := v1.FetchSeminarGroups(app)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Failed to fetch seminar groups")
return c.JSON(http.StatusBadRequest, "Failed to fetch seminar groups")
}
return c.JSON(http.StatusOK, groups)
},
@@ -72,7 +72,7 @@ func AddRoutes(app *pocketbase.PocketBase) {
sportEvents, err := sport.FetchAndUpdateSportEvents(app)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Failed to fetch sport events")
return c.JSON(http.StatusBadRequest, "Failed to fetch sport events")
}
return c.JSON(http.StatusOK, sportEvents)
},
@@ -94,7 +94,7 @@ func AddRoutes(app *pocketbase.PocketBase) {
Handler: func(c echo.Context) error {
err := events.DeleteAllEvents(app)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Failed to delete events")
return c.JSON(http.StatusBadRequest, "Failed to delete events")
}
return c.JSON(http.StatusOK, "Events deleted")
},
@@ -116,7 +116,7 @@ func AddRoutes(app *pocketbase.PocketBase) {
Handler: func(c echo.Context) error {
rooms, err := room.GetRooms(app)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Failed to get rooms")
return c.JSON(http.StatusBadRequest, "Failed to get rooms")
}
return c.JSON(http.StatusOK, rooms)
},
@@ -140,7 +140,8 @@ func AddRoutes(app *pocketbase.PocketBase) {
date := c.QueryParam("date")
roomSchedule, err := room.GetRoomScheduleForDay(app, roomParam, date)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Failed to get room schedule for day")
slog.Error("Failed to get room schedule for day: %v", err)
return c.JSON(http.StatusBadRequest, "Failed to get room schedule for day")
}
return c.JSON(http.StatusOK, roomSchedule)
},
@@ -165,7 +166,8 @@ func AddRoutes(app *pocketbase.PocketBase) {
from := c.QueryParam("from")
roomSchedule, err := room.GetRoomSchedule(app, roomParam, from, to)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Failed to get room schedule")
slog.Error("Failed to get room schedule: %v", err)
return c.JSON(http.StatusBadRequest, "Failed to get room schedule")
}
return c.JSON(http.StatusOK, roomSchedule)
},
@@ -191,9 +193,10 @@ func AddRoutes(app *pocketbase.PocketBase) {
modules, err := events.GetModulesForCourseDistinct(app, course, semester)
if err != nil {
return c.JSON(400, err)
slog.Error("Failed to get modules for course and semester: %v", err)
return c.JSON(http.StatusBadRequest, "Failed to get modules for course and semester")
} else {
return c.JSON(200, modules)
return c.JSON(http.StatusOK, modules)
}
},
Middlewares: []echo.MiddlewareFunc{
@@ -211,7 +214,12 @@ func AddRoutes(app *pocketbase.PocketBase) {
Method: http.MethodGet,
Path: "/api/modules",
Handler: func(c echo.Context) error {
return events.GetAllModulesDistinct(app, c)
modules, err := events.GetAllModulesDistinct(app)
if err != nil {
slog.Error("Failed to get modules: %v", err)
return c.JSON(http.StatusBadRequest, "Failed to get modules")
}
return c.JSON(http.StatusOK, modules)
},
Middlewares: []echo.MiddlewareFunc{
apis.ActivityLogger(app),
@@ -231,9 +239,10 @@ func AddRoutes(app *pocketbase.PocketBase) {
requestModule := c.QueryParam("uuid")
module, err := events.GetModuleByUUID(app, requestModule)
if err != nil {
return c.JSON(400, err)
slog.Error("Failed to get module: %v", err)
return c.JSON(http.StatusBadRequest, "Failed to get module")
} else {
return c.JSON(200, module)
return c.JSON(http.StatusOK, module)
}
},
Middlewares: []echo.MiddlewareFunc{
@@ -273,9 +282,10 @@ func AddRoutes(app *pocketbase.PocketBase) {
semester := c.QueryParam("semester")
err := events.DeleteAllEventsByCourseAndSemester(app, course, semester)
if err != nil {
return c.JSON(400, err)
slog.Error("Failed to delete events: %v", err)
return c.JSON(http.StatusBadRequest, "Failed to delete events")
} else {
return c.JSON(200, "Events deleted")
return c.JSON(http.StatusBadRequest, "Events deleted")
}
},
Middlewares: []echo.MiddlewareFunc{
@@ -297,9 +307,10 @@ func AddRoutes(app *pocketbase.PocketBase) {
err := ical.MigrateFeedJson(app)
if err != nil {
return c.JSON(500, err)
slog.Error("Failed to migrate feeds: %v", err)
return c.JSON(http.StatusInternalServerError, "Failed to migrate feeds")
} else {
return c.JSON(200, "Migrated")
return c.JSON(http.StatusOK, "Migrated")
}
},
Middlewares: []echo.MiddlewareFunc{