Added error handling; Made querystring helper

This commit is contained in:
Kieran Eglin 2023-11-28 12:11:14 -08:00
parent e5579b2c33
commit d9c9289d65
No known key found for this signature in database
GPG Key ID: 193984967FCF432D
3 changed files with 34 additions and 8 deletions

View File

@ -65,8 +65,10 @@
<script>
import Path from 'path'
import apiRequestHelpers from '@/mixins/apiRequestHelpers'
export default {
mixins: [apiRequestHelpers],
props: {
item: {
type: Object,
@ -132,27 +134,36 @@ export default {
}
this.isFetchingMetadata = true
this.error = ''
try {
const searchQueryString = `title=${this.itemData.title}&author=${this.itemData.author}&provider=${this.provider}`
const searchQueryString = this.buildQuerystring({
title: this.itemData.title,
author: this.itemData.author,
provider: this.provider
})
const [bestCandidate, ..._rest] = await this.$axios.$get(`/api/search/books?${searchQueryString}`)
this.itemData = {
...this.itemData,
title: bestCandidate?.title,
author: bestCandidate?.author,
series: (bestCandidate?.series || [])[0]?.series
if (bestCandidate) {
this.itemData = {
...this.itemData,
title: bestCandidate?.title,
author: bestCandidate?.author,
series: (bestCandidate?.series || [])[0]?.series
}
} else {
this.error = this.$strings.ErrorUploadFetchMetadataNoResults
}
} catch (e) {
console.error('Failed', e)
// TODO: do something with the error?
this.error = this.$strings.ErrorUploadFetchMetadataAPI
} finally {
this.isFetchingMetadata = false
}
},
getData() {
if (!this.itemData.title) {
this.error = 'Must have a title'
this.error = this.$strings.ErrorUploadLacksTitle
return null
}
this.error = ''

View File

@ -0,0 +1,12 @@
export default {
methods: {
buildQuerystring(obj, opts = { includePrefix: false }) {
let querystring = Object
.entries(obj)
.map(([key, val]) => `${encodeURIComponent(key)}=${encodeURIComponent(val)}`)
.join('&')
return (opts.includePrefix ? '?' : '').concat(querystring)
}
}
}

View File

@ -87,6 +87,9 @@
"ButtonUserEdit": "Edit user {0}",
"ButtonViewAll": "View All",
"ButtonYes": "Yes",
"ErrorUploadFetchMetadataAPI": "Error fetching metadata",
"ErrorUploadFetchMetadataNoResults": "Could not fetch metadata - try updating title and/or author",
"ErrorUploadLacksTitle": "Must have a title",
"HeaderAccount": "Account",
"HeaderAdvanced": "Advanced",
"HeaderAppriseNotificationSettings": "Apprise Notification Settings",