make new error handling work with avatar upload

This commit is contained in:
hippoz 2023-07-23 05:11:04 +03:00
parent 00f7ca72a5
commit e3c0882a2e
Signed by: hippoz
GPG key ID: 56C4E02A85F2FBED

View file

@ -2,7 +2,7 @@
import { overlayStore, userInfoStore, smallViewport, theme, doAnimations, OverlayType, sendTypingUpdatesItemStore } from "../../stores";
import { logOut } from "../../auth";
import { maybeModalFade, maybeModalScale } from "../../animations";
import request, { methods, remoteBlobUpload } from "../../request";
import request, { getErrorFromResponse, methods, remoteBlobUpload, responseOk } from "../../request";
import { apiRoute, getItem } from "../../storage";
import UserView from "../UserView.svelte";
import ChipBar from "../ChipBar.svelte";
@ -55,11 +55,13 @@
if (!avatarFileInput || !avatarFileInput.files || !avatarFileInput.files[0]) return;
const file = avatarFileInput.files[0];
const { ok } = await remoteBlobUpload(methods.putUserAvatar, file);
if (ok) {
const res = await remoteBlobUpload(methods.putUserAvatar, file);
if (responseOk(res)) {
overlayStore.toast("Your avatar has been updated");
} else {
overlayStore.toast("Failed to upload avatar");
const error = getErrorFromResponse(res);
const message = error.validationErrors && error.validationErrors.length ? error.validationErrors[0].msg : error.message;
overlayStore.toast(`Failed to upload avatar: ${message}`);
}
};