add error handling for reading configuration #1

Merged
arya merged 9 commits from odyseusz/publapi:master into master 2024-11-04 20:20:30 +05:30
2 changed files with 11 additions and 12 deletions
Showing only changes of commit dc6fb02d5f - Show all commits

View File

@ -55,7 +55,7 @@ func (a ByAdminAndName) Less(i, j int) bool {
func (a ByAdminAndName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a ByAdminAndName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func UserError(message string, username string, err error) { func UserError(message string, username string, err error) {
log.Error("error", message, log.Any("err", err), "username", username) log.Error("message", message, log.Any("err", err), "username", username)
} }
func userData(username, usersonline, ops string) UserInfo { func userData(username, usersonline, ops string) UserInfo {
@ -79,7 +79,7 @@ func userData(username, usersonline, ops string) UserInfo {
_, error := os.Stat(filename) _, error := os.Stat(filename)
if error != nil { if error != nil {
if os.IsNotExist(error) { if os.IsNotExist(error) {
log.Error(username + " does not have a meta-info.toml") UserError("user doesn't have a meta-info.toml", username, error)
var user UserInfo var user UserInfo
user.Name = username user.Name = username
user.Created, _ = strconv.Atoi(crdstr) user.Created, _ = strconv.Atoi(crdstr)
@ -98,7 +98,7 @@ func userData(username, usersonline, ops string) UserInfo {
} }
viper.SetConfigFile(filename) viper.SetConfigFile(filename)
if err := viper.ReadInConfig(); err != nil { if err := viper.ReadInConfig(); err != nil {
log.Error("message", "Couldn't read a users meta-info.toml file.", "error", log.Any("err", err), "user", username) log.Error("message", "couldn't read a users meta-info.toml file.", "error", log.Any("err", err), "user", username)
user := UserInfo{ user := UserInfo{
Name: username, Name: username,
} }
@ -175,15 +175,14 @@ func UsersPage(c *fiber.Ctx) error {
log.Error("couldn't get all users", "error", log.Any("err", err2)) log.Error("couldn't get all users", "error", log.Any("err", err2))
return c.SendStatus(fiber.StatusInternalServerError) return c.SendStatus(fiber.StatusInternalServerError)
} }
userstr := string(users) userstr := strings.TrimSuffix(string(users), "\n")
userstr2 := strings.TrimSuffix(userstr, "\n") usersarr := strings.Split(userstr, "\n")
usersarr := strings.Split(userstr2, "\n")
// Fill user info // Fill user info
var userInfoStruct []UserInfo var userInfoArray []UserInfo
for i := 0; i < len(usersarr); i++ { for i := 0; i < len(usersarr); i++ {
uname := string(usersarr[i]) uname := string(usersarr[i])
userInfoStruct = append( userInfoArray = append(
userInfoStruct, userInfoArray,
userData( userData(
uname, uname,
strings.TrimSuffix(usersOnlineDedup, "\n"), strings.TrimSuffix(usersOnlineDedup, "\n"),
@ -191,11 +190,11 @@ func UsersPage(c *fiber.Ctx) error {
), ),
) )
} }
sort.Sort(ByAdminAndName(userInfoStruct)) sort.Sort(ByAdminAndName(userInfoArray))
data := UserStruct{ data := UserStruct{
Status: c.Response().StatusCode(), Status: c.Response().StatusCode(),
Online: output, Online: output,
Users: userInfoStruct, Users: userInfoArray,
Total: int(strings.Count(userstr, "\n")), Total: int(strings.Count(userstr, "\n")),
} }
return c.JSON(data) return c.JSON(data)

View File

@ -31,7 +31,7 @@ There is a Matrix chat room for everything about the Pubnix. You can ask for hel
There is a password for Pubnix-related things in your user directory, you can run cat ~/pass to see it. We recommend to store it somewhere else and delete the file. There is a password for Pubnix-related things in your user directory, you can run cat ~/pass to see it. We recommend to store it somewhere else and delete the file.
You also get access to our public authentik instance, which allows you to sign up to many Project Segfault services with your Pubnix account. You can find it at https://auth.p.projectsegfau.lt, log in and you will be able to see a list of applications you can use authentik to log in with. You also get access to our public authentik instance, which allows you to sign up to many Project Segfault services with your Pubnix account. You can find it at https://auth.p.projectsegfau.lt, log in and you will be able to see a list of applications you can use authentik to log in with.
If you ever get confused or have no idea what to do, we have some articles on our wiki at https://wiki.projectsegfau.lt/index.php?title=Category:Pubnix. If you ever get confused or have no idea what to do, we have some articles on our wiki at https://wiki.projectsegfau.lt/t/pubnix.
You can also give us suggestions at this Vikunja page: https://pubnixtodo.p.projectsegfau.lt. You can also give us suggestions at this Vikunja page: https://pubnixtodo.p.projectsegfau.lt.
We hope you have enough freedom to do what you want. You are limited to 10GB of storage however. We hope you have enough freedom to do what you want. You are limited to 10GB of storage however.