feat: custome error messages (#14)

* feat: custome error messages

* errors: improve code

* errors: clean up imports

* errors: add translations

* errors: check if translation exists
pull/3756/head
Laurynas Gadliauskas 2021-06-18 14:05:47 +03:00 committed by GitHub
parent d5ca315102
commit 3489df033e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 148 additions and 22 deletions

View File

@ -19,3 +19,26 @@ var (
ErrSourceIsParent = errors.New("source is parent")
ErrRootUserDeletion = errors.New("user with id 1 can't be deleted")
)
type HTTPError struct {
Err error
Type string
}
func (e *HTTPError) Error() string {
if e.Err == nil {
return e.Type
}
return e.Err.Error()
}
func (e *HTTPError) Unwrap() error {
return e.Err
}
func NewHTTPError(err error, errType string) error {
return &HTTPError{
Err: err,
Type: errType,
}
}

View File

@ -76,15 +76,15 @@ export default {
try {
buttons.loading("archive");
this.$store.commit("closeHovers");
await api.archive(uri, this.name, format, ...items);
this.$store.commit("closeHovers");
this.$store.commit("setReload", true);
this.$store.dispatch("quota/fetch", 3000);
} catch (e) {
this.$showError(e);
} finally {
buttons.done("archive");
}
this.$store.dispatch("quota/fetch", 3000);
},
},
};

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "You don't have permissions to access this.",
"internal": "لقد حدث خطأ ما.",
"notFound": "لا يمكن الوصول لهذا المحتوى."
"notFound": "لا يمكن الوصول لهذا المحتوى.",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "الصفحة",

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "You don't have permissions to access this.",
"internal": "Something really went wrong.",
"notFound": "This location can't be reached."
"notFound": "This location can't be reached.",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "Body",

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "No tienes los permisos necesarios para acceder.",
"internal": "La verdad es que algo ha ido mal.",
"notFound": "No se puede acceder a este lugar."
"notFound": "No se puede acceder a este lugar.",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "Cuerpo",

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "No tienes los permisos necesarios para acceder.",
"internal": "La verdad es que algo ha ido mal.",
"notFound": "No se puede acceder a este lugar."
"notFound": "No se puede acceder a este lugar.",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "Cuerpo",

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "No tienes los permisos necesarios para acceder.",
"internal": "La verdad es que algo ha ido mal.",
"notFound": "No se puede acceder a este lugar."
"notFound": "No se puede acceder a este lugar.",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "Cuerpo",

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "No tienes los permisos necesarios para acceder.",
"internal": "La verdad es que algo ha ido mal.",
"notFound": "No se puede acceder a este lugar."
"notFound": "No se puede acceder a este lugar.",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "Cuerpo",

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "You don't have permissions to access this.",
"internal": "Aïe ! Quelque chose s'est mal passé.",
"notFound": "Impossible d'accéder à cet emplacement."
"notFound": "Impossible d'accéder à cet emplacement.",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "Corps",

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "You don't have permissions to access this.",
"internal": "Something really went wrong.",
"notFound": "This location can't be reached."
"notFound": "This location can't be reached.",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "Body",

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "You don't have permissions to access this.",
"internal": "Something really went wrong.",
"notFound": "This location can't be reached."
"notFound": "This location can't be reached.",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "Body",

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "You don't have permissions to access this.",
"internal": "Ops! Algum erro ocorreu.",
"notFound": "Ops! Nada foi encontrado."
"notFound": "Ops! Nada foi encontrado.",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "Corpo",

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "Não tem permissões para aceder a isto",
"internal": "Algo correu bastante mal.",
"notFound": "Esta localização não é alcançável."
"notFound": "Esta localização não é alcançável.",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "Corpo",

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "У вас нет прав доступа к этому.",
"internal": "Что-то пошло не так.",
"notFound": "Неправильная ссылка."
"notFound": "Неправильная ссылка.",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "Тело",

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "You don't have permissions to access this.",
"internal": "Something really went wrong.",
"notFound": "This location can't be reached."
"notFound": "This location can't be reached.",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "Body",

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "You don't have permissions to access this.",
"internal": "Something really went wrong.",
"notFound": "This location can't be reached."
"notFound": "This location can't be reached.",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "Body",

View File

@ -48,7 +48,13 @@
"connection": "The server can't be reached.",
"forbidden": "你无权限访问",
"internal": "服务器出了点问题。",
"notFound": "找不到文件。"
"notFound": "找不到文件。",
"resource": {
"alreadyExists": "Resource with such name already exists"
},
"validation": {
"emptyName": "Resource name can't be empty"
}
},
"files": {
"body": "内容",

View File

@ -45,9 +45,18 @@ Vue.prototype.$showError = (error, displayReport = true) => {
);
}
let message = error.message || error;
let matches = /\[(.+)\]/.exec(message);
if (matches && matches.length > 1) {
let key = "errors." + matches[1];
if (i18n.te(key)) {
message = i18n.t(key);
}
}
let n = new Noty(
Object.assign({}, notyDefault, {
text: error.message || error,
text: message,
type: "error",
timeout: null,
buttons: btns,

View File

@ -7,6 +7,7 @@ import (
"github.com/tomasen/realip"
"github.com/filebrowser/filebrowser/v2/errors"
"github.com/filebrowser/filebrowser/v2/rules"
"github.com/filebrowser/filebrowser/v2/runner"
"github.com/filebrowser/filebrowser/v2/settings"
@ -65,8 +66,11 @@ func handle(fn handleFunc, prefix string, store *storage.Storage, server *settin
})
if status != 0 {
txt := http.StatusText(status)
http.Error(w, strconv.Itoa(status)+" "+txt, status)
txt := strconv.Itoa(status) + " " + http.StatusText(status)
if httpErr, ok := err.(*errors.HTTPError); ok {
txt += " [" + httpErr.Type + "]"
}
http.Error(w, txt, status)
}
if status >= 400 || err != nil {

View File

@ -489,7 +489,7 @@ func archiveHandler(r *http.Request, d *data) (int, error) {
archFile, err := parseQueryFilename(r, destDir)
if err != nil {
return http.StatusBadRequest, err
return http.StatusUnprocessableEntity, errors.NewHTTPError(err, "validation.emptyName")
}
extension, ar, err := parseArchiver(r.URL.Query().Get("algo"))
@ -501,7 +501,7 @@ func archiveHandler(r *http.Request, d *data) (int, error) {
_, err = d.user.Fs.Stat(archFile)
if err == nil {
return http.StatusConflict, nil
return http.StatusUnprocessableEntity, errors.NewHTTPError(err, "resource.alreadyExists")
}
dir, _ = path.Split(archFile)