diff --git a/router/projects.go b/router/projects.go index 4945e6a..477a6f9 100644 --- a/router/projects.go +++ b/router/projects.go @@ -9,7 +9,7 @@ import ( "forge.capytal.company/capytalcode/project-comicverse/service" "forge.capytal.company/capytalcode/project-comicverse/templates" - "forge.capytal.company/loreddev/x/smalltrip/exception" + "forge.capytal.company/loreddev/x/smalltrip/problem" "forge.capytal.company/loreddev/x/tinyssert" "github.com/google/uuid" ) @@ -45,7 +45,7 @@ func (ctrl projectController) dashboard(w http.ResponseWriter, r *http.Request) projects, err := ctrl.projectSvc.GetUserProjects(userID) if err != nil { - exception.InternalServerError(err).ServeHTTP(w, r) + problem.NewInternalServerError(err).ServeHTTP(w, r) return } @@ -66,7 +66,7 @@ func (ctrl projectController) dashboard(w http.ResponseWriter, r *http.Request) err = ctrl.templates.ExecuteTemplate(w, "dashboard", ps) if err != nil { - exception.InternalServerError(err).ServeHTTP(w, r) + problem.NewInternalServerError(err).ServeHTTP(w, r) } } @@ -77,24 +77,22 @@ func (ctrl projectController) getProject(w http.ResponseWriter, r *http.Request) id, err := base64.URLEncoding.DecodeString(shortProjectID) if err != nil { - exception.BadRequest(err, exception.WithMessage("Incorrect base64 encoding of project ID")). - ServeHTTP(w, r) + problem.NewBadRequest(fmt.Sprintf("Incorrectly encoded project ID: %s", err.Error())).ServeHTTP(w, r) return } projectID, err := uuid.ParseBytes(id) if err != nil { - exception.BadRequest(err, exception.WithMessage("Incorrect project ID is not a valid UUID")). - ServeHTTP(w, r) + problem.NewBadRequest("Project ID is not a valid UUID").ServeHTTP(w, r) return } project, err := ctrl.projectSvc.GetProject(projectID) if errors.Is(err, service.ErrNotFound) { - exception.NotFound().ServeHTTP(w, r) + problem.NewNotFound().ServeHTTP(w, r) return } else if err != nil { - exception.InternalServerError(err).ServeHTTP(w, r) + problem.NewInternalServerError(err).ServeHTTP(w, r) return } @@ -103,7 +101,7 @@ func (ctrl projectController) getProject(w http.ResponseWriter, r *http.Request) w.Header().Add("Content-Type", "application/json") if _, err := w.Write(b); err != nil { - exception.InternalServerError(err).ServeHTTP(w, r) + problem.NewInternalServerError(err).ServeHTTP(w, r) return } } @@ -119,13 +117,13 @@ func (ctrl projectController) createProject(w http.ResponseWriter, r *http.Reque title := r.FormValue("title") if title == "" { - exception.BadRequest(errors.New(`missing "title"`)).ServeHTTP(w, r) + problem.NewBadRequest(`Missing "title" parameter`).ServeHTTP(w, r) return } project, err := ctrl.projectSvc.Create(title, userID) if err != nil { - exception.InternalServerError(err).ServeHTTP(w, r) + problem.NewInternalServerError(err).ServeHTTP(w, r) return } diff --git a/router/router.go b/router/router.go index ba7a3ec..d4e2d32 100644 --- a/router/router.go +++ b/router/router.go @@ -10,8 +10,8 @@ import ( "forge.capytal.company/capytalcode/project-comicverse/service" "forge.capytal.company/capytalcode/project-comicverse/templates" "forge.capytal.company/loreddev/x/smalltrip" - "forge.capytal.company/loreddev/x/smalltrip/exception" "forge.capytal.company/loreddev/x/smalltrip/middleware" + "forge.capytal.company/loreddev/x/smalltrip/problem" "forge.capytal.company/loreddev/x/tinyssert" ) @@ -100,8 +100,10 @@ func (router *router) setup() http.Handler { r.Use(middleware.DisableCache()) } - r.Use(exception.PanicMiddleware()) - r.Use(exception.Middleware()) + r.Use(problem.PanicMiddleware()) + // TODO: when the HandlerDevpage is completed on the problem package, we + // will provide it a custom template here: + // r.Use(problem.Middleware()) userController := newUserController(userControllerCfg{ UserService: router.userService, @@ -127,7 +129,7 @@ func (router *router) setup() http.Handler { err := router.templates.ExecuteTemplate(w, "landing", nil) if err != nil { - exception.InternalServerError(err).ServeHTTP(w, r) + problem.NewInternalServerError(err).ServeHTTP(w, r) } }) diff --git a/router/user.go b/router/user.go index 9335ffc..4910ac2 100644 --- a/router/user.go +++ b/router/user.go @@ -7,8 +7,8 @@ import ( "forge.capytal.company/capytalcode/project-comicverse/service" "forge.capytal.company/capytalcode/project-comicverse/templates" - "forge.capytal.company/loreddev/x/smalltrip/exception" "forge.capytal.company/loreddev/x/smalltrip/middleware" + "forge.capytal.company/loreddev/x/smalltrip/problem" "forge.capytal.company/loreddev/x/tinyssert" "github.com/golang-jwt/jwt/v4" "github.com/google/uuid" @@ -60,39 +60,38 @@ func (ctrl userController) login(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { err := ctrl.templates.ExecuteTemplate(w, "login", nil) if err != nil { - exception.InternalServerError(err).ServeHTTP(w, r) + problem.NewInternalServerError(err).ServeHTTP(w, r) } return } if r.Method != http.MethodPost { - exception.MethodNotAllowed([]string{http.MethodGet, http.MethodPost}). - ServeHTTP(w, r) + problem.NewMethodNotAllowed([]string{http.MethodGet, http.MethodPost}).ServeHTTP(w, r) return } username, passwd := r.FormValue("username"), r.FormValue("password") if username == "" { - exception.BadRequest(errors.New(`missing "username" form value`)).ServeHTTP(w, r) + problem.NewBadRequest(`Missing "username" form value`).ServeHTTP(w, r) return } if passwd == "" { - exception.BadRequest(errors.New(`missing "password" form value`)).ServeHTTP(w, r) + problem.NewBadRequest(`Missing "password" form value`).ServeHTTP(w, r) return } // TODO: Move token issuing to it's own service, make UserService.Login just return the user user, err := ctrl.userSvc.Login(username, passwd) if errors.Is(err, service.ErrNotFound) { - exception.NotFound(exception.WithError(errors.New("user not found"))).ServeHTTP(w, r) + problem.NewNotFound().ServeHTTP(w, r) return } else if err != nil { - exception.InternalServerError(err).ServeHTTP(w, r) + problem.NewInternalServerError(err).ServeHTTP(w, r) return } token, err := ctrl.tokenSvc.Issue(user) if err != nil { - exception.InternalServerError(err).ServeHTTP(w, r) + problem.NewInternalServerError(err).ServeHTTP(w, r) return } @@ -115,38 +114,38 @@ func (ctrl userController) register(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodGet { err := ctrl.templates.ExecuteTemplate(w, "register", nil) if err != nil { - exception.InternalServerError(err).ServeHTTP(w, r) + problem.NewInternalServerError(err).ServeHTTP(w, r) } return } if r.Method != http.MethodPost { - exception.MethodNotAllowed([]string{http.MethodGet, http.MethodPost}).ServeHTTP(w, r) + problem.NewMethodNotAllowed([]string{http.MethodGet, http.MethodPost}).ServeHTTP(w, r) return } username, passwd := r.FormValue("username"), r.FormValue("password") if username == "" { - exception.BadRequest(errors.New(`missing "username" form value`)).ServeHTTP(w, r) + problem.NewBadRequest(`Missing "username" form value`).ServeHTTP(w, r) return } if passwd == "" { - exception.BadRequest(errors.New(`missing "password" form value`)).ServeHTTP(w, r) + problem.NewBadRequest(`Missing "password" form value`).ServeHTTP(w, r) return } user, err := ctrl.userSvc.Register(username, passwd) if errors.Is(err, service.ErrUsernameAlreadyExists) || errors.Is(err, service.ErrPasswordTooLong) { - exception.BadRequest(err).ServeHTTP(w, r) + problem.NewBadRequest(err.Error()).ServeHTTP(w, r) return } else if err != nil { - exception.InternalServerError(err).ServeHTTP(w, r) + problem.NewInternalServerError(err).ServeHTTP(w, r) return } token, err := ctrl.tokenSvc.Issue(user) if err != nil { - exception.InternalServerError(err).ServeHTTP(w, r) + problem.NewInternalServerError(err).ServeHTTP(w, r) return } @@ -214,15 +213,14 @@ func (ctx UserContext) Unathorize(w http.ResponseWriter, r *http.Request) { // Since we use HTMX, we can't just return a redirect response probably, // the framework will just get the login page html and not redirect the user to the page. - msg := `The "Authorization" header or "authorization" cookie must be present with a valid token` - var excep exception.Exception + var p problem.Problem if err, ok := ctx.GetTokenErr(); ok { - excep = exception.Unathorized(msg, exception.WithError(err)) + p = problem.NewUnauthorized(problem.AuthSchemeBearer, problem.WithError(err)) } else { - excep = exception.Unathorized(msg) + p = problem.NewUnauthorized(problem.AuthSchemeBearer) } - excep.ServeHTTP(w, r) + p.ServeHTTP(w, r) } func (ctx UserContext) GetUserID() (uuid.UUID, bool) { diff --git a/x b/x index c62be87..982c2a4 160000 --- a/x +++ b/x @@ -1 +1 @@ -Subproject commit c62be87c6a92e4a96ff039245924cf09748aba55 +Subproject commit 982c2a4cb5358d33969a8501ec22d3d71a7cc1cd