Skip to content

throw errors rather than resolving them #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Changes from all 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: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
interface CachedData {
src: string
data: Promise<string | CSPTrustedHTMLToStringable | Error>
data: Promise<string | CSPTrustedHTMLToStringable>
}
const privateData = new WeakMap<IncludeFragmentElement, CachedData>()

Expand Down Expand Up @@ -65,7 +65,7 @@ export default class IncludeFragmentElement extends HTMLElement {

// We will return string or error for API backwards compatibility. We can consider
// returning TrustedHTML in the future.
get data(): Promise<string | Error> {
get data(): Promise<string> {
return this.#getStringOrErrorData()
}

Expand Down Expand Up @@ -117,7 +117,7 @@ export default class IncludeFragmentElement extends HTMLElement {
})
}

load(): Promise<string | Error> {
load(): Promise<string> {
return this.#getStringOrErrorData()
}

Expand Down Expand Up @@ -177,13 +177,13 @@ export default class IncludeFragmentElement extends HTMLElement {
}
}

async #getData(): Promise<string | CSPTrustedHTMLToStringable | Error> {
async #getData(): Promise<string | CSPTrustedHTMLToStringable> {
const src = this.src
const cachedData = privateData.get(this)
if (cachedData && cachedData.src === src) {
return cachedData.data
} else {
let data: Promise<string | CSPTrustedHTMLToStringable | Error>
let data: Promise<string | CSPTrustedHTMLToStringable>
if (src) {
data = this.#fetchDataWithEvents()
} else {
Expand All @@ -194,10 +194,10 @@ export default class IncludeFragmentElement extends HTMLElement {
}
}

async #getStringOrErrorData(): Promise<string | Error> {
async #getStringOrErrorData(): Promise<string> {
const data = await this.#getData()
if (data instanceof Error) {
return data
throw data
}
Comment on lines 199 to 201
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might now be unreachable and safe to remove?

return data.toString()
}
Expand Down