-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from rtablada/error-response
Consider 4xx and 5xx status as error instead of trying to render MD
- Loading branch information
Showing
10 changed files
with
176 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { renderSettled } from '@ember/renderer'; | ||
import { click, settled, visit } from '@ember/test-helpers'; | ||
import { module, test } from 'qunit'; | ||
import { setupApplicationTest } from 'ember-qunit'; | ||
|
||
module('Errors', function (hooks) { | ||
setupApplicationTest(hooks); | ||
|
||
module('Not found', function () { | ||
test('not in any manifest / group', async function (assert) { | ||
await visit('/usage/does-not-exist.md'); | ||
|
||
assert.dom().doesNotContainText(`Cannot GET`, 'does not directly expose errors from fetch'); | ||
assert.dom().doesNotContainText(`null`, 'does not incorrectly render the error'); | ||
|
||
assert.dom(`[data-page-error]`).containsText(`Page not found for path`); | ||
assert.dom(`[data-page-error]`).containsText(`/usage/does-not-exist`); | ||
assert.dom(`[data-page-error]`).containsText(`Using group: root`); | ||
}); | ||
|
||
test(`can recover after an error`, async function (assert) { | ||
await visit('/usage/does-not-exist.md'); | ||
|
||
assert.dom(`[data-page-error]`).containsText(`Page not found for path`); | ||
|
||
await click(`a[href="/usage/setup.md"]`); | ||
|
||
assert.dom().doesNotContainText(`Page not found for path`); | ||
}); | ||
|
||
test(`attempting to visit a route that doesn't exist in the current group, but does exist in another group`, async function (assert) { | ||
await visit(`/Runtime/docs/api-docs.md`); | ||
|
||
assert.dom().doesNotContainText(`Page not found for path`); | ||
}); | ||
|
||
test('the error does not flash between known pages rendering', async function (assert) { | ||
visit(`/Runtime/docs/api-docs.md`); | ||
|
||
await renderSettled(); | ||
assert.dom('[data-page-error]').doesNotExist(); | ||
|
||
await settled(); | ||
assert.dom('[data-page-error]').doesNotExist(); | ||
|
||
visit(`/Runtime/util/logs.md`); | ||
|
||
await renderSettled(); | ||
assert.dom('[data-page-error]').doesNotExist(); | ||
|
||
await settled(); | ||
assert.dom('[data-page-error]').doesNotExist(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { cached } from '@glimmer/tracking'; | ||
import { service } from '@ember/service'; | ||
|
||
import { use } from 'ember-resources'; | ||
import { keepLatest } from 'reactiveweb/keep-latest'; | ||
import { RemoteData } from 'reactiveweb/remote-data'; | ||
|
||
import type DocsService from './docs'; | ||
|
||
export const OUTPUT_PREFIX = `/docs/`; | ||
export const OUTPUT_PREFIX_REGEX = /^\/docs\//; | ||
|
||
/** | ||
* With how data is derived here, the `fetch` request does not execute | ||
* if we know ahead of time that the fetch would fail. | ||
* e.g.: when the URL is not declared in the manifest. | ||
* | ||
* The `fetch` only occurs when `last` is accessd. | ||
* and `last` is not accessed if `doesPageExist` is ever false. | ||
*/ | ||
export class MDRequest { | ||
constructor(private urlFn: () => string) {} | ||
|
||
@service('kolay/docs') declare docs: DocsService; | ||
|
||
/** | ||
* TODO: use a private property when we move to spec-decorators | ||
*/ | ||
@use last = RemoteData<string>(this.urlFn); | ||
|
||
@use lastSuccessful = keepLatest({ | ||
value: () => this.#lastValue, | ||
when: () => this.hasError, | ||
}); | ||
|
||
get hasError() { | ||
if (!this._doesPageExist) return true; | ||
|
||
/** | ||
* Can't have an error if we haven't made a request yet | ||
*/ | ||
if (!this.last.status) return false; | ||
|
||
return this.last.status >= 400; | ||
} | ||
|
||
/** | ||
* TODO: use a private property when we move to spec-decorators | ||
*/ | ||
@cached | ||
private get _doesPageExist() { | ||
let url = this.urlFn(); | ||
let pagePath = url.replace(OUTPUT_PREFIX_REGEX, '/'); | ||
let group = this.docs.groupForURL(pagePath); | ||
|
||
return Boolean(group); | ||
} | ||
|
||
get #lastValue() { | ||
if (!this._doesPageExist) return ''; | ||
|
||
return this.last.value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters