Stian Aune
5 years ago
7 changed files with 130 additions and 7 deletions
-
2cmd/lucifer-server/main.go
-
25controllers/user-controller.go
-
1go.mod
-
2go.sum
-
55internal/httperr/error.go
-
33middlewares/session.go
-
19models/user.go
@ -0,0 +1,55 @@ |
|||
package httperr |
|||
|
|||
import ( |
|||
"fmt" |
|||
"log" |
|||
"net/http" |
|||
"strconv" |
|||
"time" |
|||
|
|||
"git.aiterp.net/lucifer/lucifer/internal/respond" |
|||
"github.com/google/uuid" |
|||
) |
|||
|
|||
// Error is an error that can be used.
|
|||
type Error struct { |
|||
Status int |
|||
Kind string |
|||
Message string |
|||
} |
|||
|
|||
func (err Error) Error() string { |
|||
return err.Kind + ": " + err.Message |
|||
} |
|||
|
|||
// ErrLoginRequired is a common error for when a session is expected, but none is found.
|
|||
var ErrLoginRequired = Error{Status: 401, Kind: "login_required", Message: "You are not logged in."} |
|||
|
|||
// NotFound generates a 404 error.
|
|||
func NotFound(model string) Error { |
|||
return Error{Status: 404, Kind: "not_found", Message: model + " not found"} |
|||
} |
|||
|
|||
// Respond responds with the error using the format in the `respond` package. If
|
|||
// the error is not a httperr.Error, then it'll be logged and a 500 will be returned.
|
|||
func Respond(w http.ResponseWriter, err error) { |
|||
if httpErr, ok := err.(Error); ok { |
|||
respond.Error(w, httpErr.Status, httpErr.Kind, httpErr.Message) |
|||
} else { |
|||
errIDStr := "" |
|||
errID, err2 := uuid.NewRandom() |
|||
if err2 != nil { |
|||
errID, err2 = uuid.NewUUID() |
|||
if err2 != nil { |
|||
errIDStr = strconv.FormatInt(time.Now().UnixNano(), 36) |
|||
} else { |
|||
errIDStr = errID.String() |
|||
} |
|||
} else { |
|||
errIDStr = errID.String() |
|||
} |
|||
|
|||
log.Printf("ERROR [%s]: %s", errIDStr, err.Error()) |
|||
respond.Error(w, 500, "internal_error", fmt.Sprintf("Something went wrong. It has been logged with the ID %s", errIDStr)) |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue