From 46f925663e595fa8dd87f7aa15a0061abcfae3d7 Mon Sep 17 00:00:00 2001 From: Michael Hunteman Date: Mon, 2 Sep 2024 16:27:12 -0700 Subject: Add ui error messages --- server/admin/handler.go | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) (limited to 'server/admin') diff --git a/server/admin/handler.go b/server/admin/handler.go index f2d5807..b05fc88 100644 --- a/server/admin/handler.go +++ b/server/admin/handler.go @@ -29,7 +29,8 @@ func NewAdminHandler(adminStore adminStore, guestStore guest.GuestStore) *AdminH return &AdminHandler{adminStore, guestStore} } -func (adminHandler *AdminHandler) ServeHTTP(responseWriter http.ResponseWriter, request *http.Request) { +func (adminHandler *AdminHandler) ServeHTTP(responseWriter http.ResponseWriter, + request *http.Request) { switch { case request.Method == http.MethodOptions: responseWriter.WriteHeader(http.StatusOK) @@ -40,7 +41,8 @@ func (adminHandler *AdminHandler) ServeHTTP(responseWriter http.ResponseWriter, } } -func (adminHandler *AdminHandler) handleLogIn(responseWriter http.ResponseWriter, request *http.Request) { +func (adminHandler *AdminHandler) handleLogIn(responseWriter http.ResponseWriter, + request *http.Request) { token, err := adminHandler.logIn(request) if err != nil { http.Error(responseWriter, err.Message, err.Code) @@ -52,29 +54,35 @@ func (adminHandler *AdminHandler) handleLogIn(responseWriter http.ResponseWriter func (adminHandler *AdminHandler) logIn(request *http.Request) ([]byte, *appError) { requestAdmin, err := adminHandler.decodeCredentials(request) if err != nil { - return []byte{}, &appError{err, "failed to unmarshal request", http.StatusBadRequest} + return []byte{}, &appError{err, "{ \"message\": \"Failed to unmarshal request\" }", + http.StatusBadRequest} } _, err = adminHandler.adminStore.Find(requestAdmin) if err != nil { - return []byte{}, &appError{err, "admin not found", http.StatusUnauthorized} + return []byte{}, &appError{err, "{ \"message\": \"Admin not found\" }", + http.StatusUnauthorized} } expirationTime := adminHandler.setExpirationTime() claims := adminHandler.createClaims(requestAdmin, expirationTime) key, err := adminHandler.readKey() if err != nil { - return []byte{}, &appError{err, "failed to read secret key", http.StatusInternalServerError} + return []byte{}, &appError{err, "{ \"message\": \"Failed to read secret key\" }", + http.StatusInternalServerError} } token, err := adminHandler.createToken(claims, key) if err != nil { - return []byte{}, &appError{err, "failed to create token", http.StatusInternalServerError} + return []byte{}, &appError{err, "{ \"message\": \"Failed to create token\" }", + http.StatusInternalServerError} } guests, err := adminHandler.guestStore.Get() if err != nil { - return []byte{}, &appError{err, "failed to get guests", http.StatusInternalServerError} + return []byte{}, &appError{err, "{ \"message\": \"Failed to get guests\" }", + http.StatusInternalServerError} } jsonBytes, err := adminHandler.marshalResponse(guests, token) if err != nil { - return []byte{}, &appError{err, "failed to marshal response", http.StatusInternalServerError} + return []byte{}, &appError{err, "{ \"message\": \"Failed to marshal response\" }", + http.StatusInternalServerError} } return jsonBytes, nil } @@ -109,11 +117,13 @@ func (adminHandler *AdminHandler) createToken(claims *Claims, key []byte) (strin return token.SignedString(key) } -func (adminHandler *AdminHandler) marshalResponse(guests []guest.Guest, token string) ([]byte, error) { +func (adminHandler *AdminHandler) marshalResponse(guests []guest.Guest, + token string) ([]byte, error) { loginResponse := adminHandler.createLoginResponse(guests, token) return json.Marshal(loginResponse) } -func (adminHandler *AdminHandler) createLoginResponse(guests []guest.Guest, token string) *Login { +func (adminHandler *AdminHandler) createLoginResponse(guests []guest.Guest, + token string) *Login { return &Login{guests, token} } -- cgit v1.2.3