Improve error handling when uploading files to S3
This commit is contained in:
parent
375d03a2d6
commit
73f2c9bd35
3 changed files with 14 additions and 3 deletions
|
@ -1,4 +1,5 @@
|
||||||
import API from "./api"
|
import API from "./api"
|
||||||
|
import { notificationStore } from "../stores/index.js"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uploads an attachment to the server.
|
* Uploads an attachment to the server.
|
||||||
|
@ -22,6 +23,9 @@ export const getSignedDatasourceURL = async (datasourceId, bucket, key) => {
|
||||||
url: `/api/attachments/${datasourceId}/url`,
|
url: `/api/attachments/${datasourceId}/url`,
|
||||||
body: { bucket, key },
|
body: { bucket, key },
|
||||||
})
|
})
|
||||||
|
if (res.error) {
|
||||||
|
throw "Could not generate signed upload URL"
|
||||||
|
}
|
||||||
return res?.signedUrl
|
return res?.signedUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,10 +34,13 @@ export const getSignedDatasourceURL = async (datasourceId, bucket, key) => {
|
||||||
*/
|
*/
|
||||||
export const externalUpload = async (datasourceId, bucket, key, data) => {
|
export const externalUpload = async (datasourceId, bucket, key, data) => {
|
||||||
const signedUrl = await getSignedDatasourceURL(datasourceId, bucket, key)
|
const signedUrl = await getSignedDatasourceURL(datasourceId, bucket, key)
|
||||||
await API.put({
|
const res = await API.put({
|
||||||
url: signedUrl,
|
url: signedUrl,
|
||||||
body: data,
|
body: data,
|
||||||
json: false,
|
json: false,
|
||||||
external: true,
|
external: true,
|
||||||
})
|
})
|
||||||
|
if (res.error) {
|
||||||
|
throw "Could not upload file to signed URL"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,12 @@
|
||||||
|
|
||||||
const upload = async () => {
|
const upload = async () => {
|
||||||
loading = true
|
loading = true
|
||||||
await API.externalUpload(datasourceId, bucket, key, data)
|
try {
|
||||||
|
await API.externalUpload(datasourceId, bucket, key, data)
|
||||||
|
notificationStore.actions.success("File uploaded successfully")
|
||||||
|
} catch (error) {
|
||||||
|
notificationStore.actions.error(`Error uploading file: ${error}`)
|
||||||
|
}
|
||||||
loading = false
|
loading = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -165,7 +165,6 @@ const s3UploadHandler = async action => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
await uploadStore.actions.processFileUpload(componentId)
|
await uploadStore.actions.processFileUpload(componentId)
|
||||||
notificationStore.actions.success("File uploaded successfully")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const handlerMap = {
|
const handlerMap = {
|
||||||
|
|
Loading…
Reference in a new issue