Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

perf: improve the prompt for upload failure of the upload component #769

Merged
merged 3 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/components/upload/UppyUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { Restrictions } from "@uppy/core";
import XHRUpload from "@uppy/xhr-upload";
import zh_CN from "@uppy/locales/lib/zh_CN";
import { computed, onUnmounted } from "vue";
import { Toast } from "@halo-dev/components";
import type { ProblemDetail } from "@/utils/api-client";

const props = withDefaults(
defineProps<{
Expand Down Expand Up @@ -50,6 +52,18 @@ const uppy = computed(() => {
fieldName: props.name,
method: props.method,
limit: 5,
getResponseError: (responseText) => {
const response = JSON.parse(responseText);
const { title, detail } = (response || {}) as ProblemDetail;
const message = [title, detail].filter(Boolean).join(": ");

if (message) {
Toast.error(message, { duration: 5000 });

return new Error(message);
}
return new Error("Internal Server Error");
},
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/utils/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const axiosInstance = axios.create({
withCredentials: true,
});

interface ProblemDetail {
export interface ProblemDetail {
detail: string;
instance: string;
status: number;
Expand Down