From f7bf818d29393ceaccf4d2906557351fa6a4f49f Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Sat, 7 May 2022 21:21:38 -0700 Subject: added error func and general cleanup/organization --- util/util.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'util') diff --git a/util/util.go b/util/util.go index 0126556..9c1ba97 100644 --- a/util/util.go +++ b/util/util.go @@ -4,6 +4,7 @@ import ( "crypto/sha256" "database/sql" "encoding/hex" + "errors" "fmt" "io/ioutil" "mime/multipart" @@ -213,7 +214,7 @@ func CreateUniqueID(actor string) (string, error) { args := fmt.Sprintf("%s/%s/%s", config.Domain, actor, newID) rows, err := config.DB.Query(query, args) if err != nil { - return "", err + return "", MakeError(err, "CreateUniqueID") } defer rows.Close() @@ -236,7 +237,7 @@ func GetFileContentType(out multipart.File) (string, error) { _, err := out.Read(buffer) if err != nil { - return "", err + return "", MakeError(err, "GetFileContentType") } out.Seek(0, 0) @@ -269,7 +270,7 @@ func LoadThemes() { // get list of themes themes, err := ioutil.ReadDir("./static/css/themes") if err != nil { - panic(err) + MakeError(err, "LoadThemes") } for _, f := range themes { @@ -287,14 +288,14 @@ func GetBoardAuth(board string) ([]string, error) { var rows *sql.Rows var err error if rows, err = config.DB.Query(query, board); err != nil { - return auth, err + return auth, MakeError(err, "GetBoardAuth") } defer rows.Close() for rows.Next() { var _type string if err := rows.Scan(&_type); err != nil { - return auth, err + return auth, MakeError(err, "GetBoardAuth") } auth = append(auth, _type) @@ -302,3 +303,12 @@ func GetBoardAuth(board string) ([]string, error) { return auth, nil } + +func MakeError(err error, msg string) error { + if err != nil { + s := fmt.Sprintf("%s: %s", msg, err.Error()) + return errors.New(s) + } + + return nil +} -- cgit v1.2.3