Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5e62b07

Browse filesBrowse files
committed
fix(core): reading resource value after reload in the error state
When the resource is loading after reloading from the error state reading `Resource.value()` would return the default value instead of throwing an error. This change prevents `Resource.hasValue()` from throwing an error in such a case. BREAKING CHANGE: * `Resource.value()` now returns a default value when in a loading state after reloading the error state
1 parent a0cd72f commit 5e62b07
Copy full SHA for 5e62b07

File tree

Expand file treeCollapse file tree

2 files changed

+49
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+49
-0
lines changed

‎packages/core/src/resource/resource.ts

Copy file name to clipboardExpand all lines: packages/core/src/resource/resource.ts
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ export class ResourceImpl<T, R> extends BaseWritableResource<T> implements Resou
170170
return defaultValue;
171171
}
172172

173+
// Prevents `hasValue()` from throwing an error when a reload happened in the error state
174+
if (this.state().status === 'loading' && this.error()) {
175+
return defaultValue;
176+
}
177+
173178
if (!isResolved(streamValue)) {
174179
throw new Error('Resource is currently in the error state', {cause: this.error()});
175180
}

‎packages/core/test/resource/resource_spec.ts

Copy file name to clipboardExpand all lines: packages/core/test/resource/resource_spec.ts
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,50 @@ describe('resource', () => {
794794
expect(echoResource.error()).toBe(undefined);
795795
expect(aborted).toEqual([{counter: 0}, {counter: 0}]);
796796
});
797+
798+
it('should return default value when getting a value after reloading in the error state', async () => {
799+
const backend = new MockEchoBackend();
800+
const requestParam = {};
801+
const echoResource = resource({
802+
params: () => requestParam,
803+
loader: (params) => backend.fetch(params.params),
804+
injector: TestBed.inject(Injector),
805+
defaultValue: 'my-default-value',
806+
});
807+
808+
TestBed.tick();
809+
await backend.reject(requestParam, 'Something went wrong....');
810+
811+
expect(echoResource.status()).toBe('error');
812+
expect(echoResource.isLoading()).toBeFalse();
813+
expect(echoResource.hasValue()).toBeFalse();
814+
expect(() => echoResource.value()).toThrow(
815+
new Error('Resource is currently in the error state'),
816+
);
817+
expect(echoResource.error()).toEqual(
818+
new Error('Unknown error', {cause: 'Something went wrong....'}),
819+
);
820+
821+
echoResource.reload();
822+
TestBed.tick();
823+
824+
expect(echoResource.status()).toBe('reloading');
825+
expect(echoResource.isLoading()).toBeTrue();
826+
expect(echoResource.hasValue()).toBeTrue();
827+
expect(echoResource.value()).toEqual('my-default-value');
828+
expect(echoResource.error()).toEqual(
829+
new Error('Unknown error', {cause: 'Something went wrong....'}),
830+
);
831+
832+
await backend.flush();
833+
TestBed.tick();
834+
835+
expect(echoResource.status()).toBe('resolved');
836+
expect(echoResource.isLoading()).toBeFalse();
837+
expect(echoResource.hasValue()).toBeTrue();
838+
expect(echoResource.value()).toEqual({});
839+
expect(echoResource.error()).toEqual(undefined);
840+
});
797841
});
798842

799843
function flushMicrotasks(): Promise<void> {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.