Skip to content

Commit

Permalink
fix: improve error readability (#79)
Browse files Browse the repository at this point in the history
Co-authored-by: Pooya Parsa <[email protected]>
  • Loading branch information
farnabaz and pi0 authored Nov 13, 2020
1 parent 969c7e7 commit 763f215
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/runtime/meta.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { RuntimeImageInfo } from 'types'

export async function getMeta (url, cache): Promise<RuntimeImageInfo> {
const cacheKey = 'image:meta:' + url
if (cache.has(cacheKey)) {
return cache.get(cacheKey)
}

async function _getMeta (url): Promise<RuntimeImageInfo> {
if (process.client) {
if (typeof Image === 'undefined') {
throw new TypeError('Image not supported')
Expand All @@ -19,7 +14,6 @@ export async function getMeta (url, cache): Promise<RuntimeImageInfo> {
height: img.height,
placeholder: url
}
cache.set(cacheKey, meta)
resolve(meta)
}
img.onerror = err => reject(err)
Expand All @@ -36,7 +30,27 @@ export async function getMeta (url, cache): Promise<RuntimeImageInfo> {
height,
placeholder: `data:${mimeType};base64,${data.toString('base64')}`
}
cache.set(cacheKey, meta)

return meta
}
}

export async function getMeta (url, cache): Promise<RuntimeImageInfo> {
const cacheKey = 'image:meta:' + url
if (cache.has(cacheKey)) {
return cache.get(cacheKey)
}

const meta = await _getMeta(url).catch((err) => {
// eslint-disable-next-line no-console
console.error('Failed to get image meta for ' + url, err + '')
return {
width: 0,
height: 0,
placeholder: ''
}
})

cache.set(cacheKey, meta)
return meta
}

0 comments on commit 763f215

Please sign in to comment.