Skip to content

Commit

Permalink
feat: improve extension errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaUnknown committed Jul 3, 2024
1 parent c344d54 commit 3ca45b5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 23 deletions.
2 changes: 0 additions & 2 deletions common/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import ViewAnime from './views/ViewAnime/ViewAnime.svelte'
import TorrentModal from './views/TorrentSearch/TorrentModal.svelte'
import Menubar from './components/Menubar.svelte'
import IspBlock from './views/IspBlock.svelte'
import { Toaster } from 'svelte-sonner'
import Logout from './components/Logout.svelte'
import Navbar from './components/Navbar.svelte'
Expand All @@ -31,7 +30,6 @@
</script>

<div class='page-wrapper with-transitions bg-dark position-relative' data-sidebar-type='overlayed-all'>
<IspBlock />
<Menubar bind:page={$page} />
<ViewAnime />
<Logout />
Expand Down
4 changes: 4 additions & 0 deletions common/css.css
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
/* --error-text: hsl(358, 100%, 81%); */
}

[data-sonner-toaster] [data-description] {
white-space: pre-wrap;
}

.z-100 {
z-index: 100;
}
Expand Down
10 changes: 9 additions & 1 deletion common/modules/extensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { anilistClient } from '../anilist.js'
import { anitomyscript } from '../anime.js'
import { client } from '@/modules/torrent.js'
import { extensionsWorker } from '@/views/Settings/TorrentSettings.svelte'
import { toast } from 'svelte-sonner'

/** @typedef {import('@thaunknown/ani-resourced/sources/types.d.ts').Options} Options */
/** @typedef {import('@thaunknown/ani-resourced/sources/types.d.ts').Result} Result */
Expand Down Expand Up @@ -33,7 +34,14 @@ export default async function getResultsFromExtensions ({ media, episode, batch,
exclusions
}

const results = await worker.query(options, { movie, batch }, settings.value.sources)
const { results, errors } = await worker.query(options, { movie, batch }, settings.value.sources)

for (const error of errors) {
console.error(error)
toast.error('Source Fetch Failed!', {
description: error
})
}

const deduped = dedupe(results)

Expand Down
47 changes: 37 additions & 10 deletions common/modules/extensions/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,51 @@ class Extensions {

/**
* @param {Options} options
* @param {{ movie: boolean, batch: boolean }} param1
* @param {Record<string, boolean>} sources
* @param {{ movie: boolean, batch: boolean }} types
* @param {Record<string, boolean>} sourcesToQuery
*/
async query (options, { movie, batch }, sources) {
/** @type {Promise<Result[]>[]} */
async query (options, types, sourcesToQuery) {
/** @type {Promise<{ results: Result[], errors: string[] }>[]} */
const promises = []
for (const source of Object.values(this.sources)) {
if (!sources[source.name]) continue
if (movie) promises.push(source.movie(options))
if (batch) promises.push(source.batch(options))
promises.push(source.single(options))
if (!sourcesToQuery[source.name]) continue
promises.push(this._querySource(source, options, types))
}
/** @type {Result[]} */
const results = []
const errors = []
for (const res of await Promise.all(promises)) {
results.push(...res.results)
errors.push(...res.errors)
}

return { results, errors }
}

/**
* @param {AbstractSource} source
* @param {Options} options
* @param {{ movie: boolean, batch: boolean }} types
* @returns {Promise<{ results: Result[], errors: string[] }>}
*/
async _querySource (source, options, { movie, batch }) {
const promises = []
promises.push(source.single(options))
if (movie) promises.push(source.movie(options))
if (batch) promises.push(source.batch(options))

const results = []
const errors = []
for (const result of await Promise.allSettled(promises)) {
if (result.status === 'fulfilled') results.push(...result.value)
if (result.status === 'fulfilled') {
results.push(...result.value)
} else {
console.error(result)
errors.push('Source ' + source.name + ' failed to load results:\n' + result.reason.message)
}
}
return results.flat()

return { results, errors }
}
}

Expand Down
16 changes: 6 additions & 10 deletions common/modules/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,11 @@ export function getReleasesRSSurl (val) {

export async function getRSSContent (url) {
if (!url) return null
try {
const res = await fetch(url)
if (!res.ok) {
throw new Error('Failed fetching RSS!\n' + res.statusText)
}
return DOMPARSER(await res.text(), 'text/xml')
} catch (e) {
throw new Error('Failed fetching RSS!\n' + e.message)
const res = await fetch(url)
if (!res.ok) {
throw new Error(res.statusText)
}
return DOMPARSER(await res.text(), 'text/xml')
}

class RSSMediaManager {
Expand All @@ -73,9 +69,9 @@ class RSSMediaManager {
if (!ignoreErrors) {
res.catch(e => {
toast.error('Search Failed', {
description: 'Failed fetching RSS!\n' + e.message
description: 'Failed Loading Media for Home Feed!\n' + e.message
})
console.error('Failed to fetch rss', e)
console.error('Failed Loading Media for Home Feed', e)
})
}
return Array.from({ length: perPage }, (_, i) => ({ type: 'episode', data: this.fromPending(res, i) }))
Expand Down

0 comments on commit 3ca45b5

Please sign in to comment.