feat: custome error messages (#14)
* feat: custome error messages * errors: improve code * errors: clean up imports * errors: add translations * errors: check if translation existspull/3756/head
parent
d5ca315102
commit
3489df033e
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
|
@ -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": "الصفحة",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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": "Тело",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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": "内容",
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue