diff --git a/src/include-fragment-element.ts b/src/include-fragment-element.ts index c8e0280..64725cf 100644 --- a/src/include-fragment-element.ts +++ b/src/include-fragment-element.ts @@ -234,13 +234,13 @@ export class IncludeFragmentElement extends HTMLElement { try { await this.#task(['loadstart']) const response = await this.fetch(this.request()) - if (response.status !== 200) { - throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`) - } const ct = response.headers.get('Content-Type') if (!isWildcard(this.accept) && (!ct || !ct.includes(this.accept ? this.accept : 'text/html'))) { throw new Error(`Failed to load resource: expected ${this.accept || 'text/html'} but was ${ct}`) } + if (response.status !== 200) { + throw new Error(`Failed to load resource: the server responded with a status of ${response.status}`) + } const responseText: string = await response.text() let data: string | CSPTrustedHTMLToStringable = responseText diff --git a/test/test.js b/test/test.js index 19066e1..9796278 100644 --- a/test/test.js +++ b/test/test.js @@ -213,7 +213,7 @@ suite('include-fragment-element', function () { await el.data throw new Error('el.data did not throw') } catch (error) { - assert.match(error, /the server responded with a status of 406/) + assert.match(error, /expected text\/html but was text\/plain/) } }) @@ -343,7 +343,10 @@ suite('include-fragment-element', function () { assert.equal(event.bubbles, false) assert.equal(event.cancelable, false) assert.instanceOf(event.detail.error, Error) - assert.equal(event.detail.error.message, 'Failed to load resource: the server responded with a status of 500') + assert.equal( + event.detail.error.message, + 'Failed to load resource: expected text/html but was text/plain;charset=UTF-8', + ) }) test('adds is-error class on 500 status', async function () {