Content-Length: 394890 | pFad | https://github.com/angular/angular/commit/6e79eaf7399170611bcbefda0082947b629f2693

FE fix(core): reading resource value after reload in the error state (#6… · angular/angular@6e79eaf · GitHub
Skip to content

Commit 6e79eaf

Browse files
Humberdatscott
authored andcommitted
fix(core): reading resource value after reload in the error state (#61441)
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. PR Close #61441
1 parent 3ba39bc commit 6e79eaf

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

packages/core/src/resource/resource.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ export class ResourceImpl<T, R> extends BaseWritableResource<T> implements Resou
187187
return defaultValue;
188188
}
189189

190+
// Prevents `hasValue()` from throwing an error when a reload happened in the error state
191+
if (this.state().status === 'loading' && this.error()) {
192+
return defaultValue;
193+
}
194+
190195
if (!isResolved(streamValue)) {
191196
if (throwErrorsFromValue) {
192197
throw new ResourceValueError(this.error()!);

packages/core/test/resource/resource_spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,48 @@ describe('resource', () => {
835835
expect(echoResource.error()).toBe(undefined);
836836
expect(aborted).toEqual([{counter: 0}, {counter: 0}]);
837837
});
838+
839+
it('should return default value when getting a value after reloading in the error state', async () => {
840+
const backend = new MockEchoBackend();
841+
const requestParam = {};
842+
const echoResource = resource({
843+
params: () => requestParam,
844+
loader: (params) => backend.fetch(params.params),
845+
injector: TestBed.inject(Injector),
846+
defaultValue: 'my-default-value',
847+
});
848+
849+
TestBed.tick();
850+
await backend.reject(requestParam, new Error('Something went wrong....'));
851+
852+
expect(echoResource.status()).toBe('error');
853+
expect(echoResource.isLoading()).toBeFalse();
854+
expect(echoResource.hasValue()).toBeFalse();
855+
856+
const errFromValue = extractError(() => echoResource.value())!;
857+
expect(errFromValue.message).toContain('Resource');
858+
expect(errFromValue.message).toContain('Something went wrong....');
859+
expect(errFromValue.cause).toEqual(new Error('Something went wrong....'));
860+
expect(echoResource.error()!).toEqual(new Error('Something went wrong....'));
861+
862+
echoResource.reload();
863+
TestBed.tick();
864+
865+
expect(echoResource.status()).toBe('reloading');
866+
expect(echoResource.isLoading()).toBeTrue();
867+
expect(echoResource.hasValue()).toBeTrue();
868+
expect(echoResource.value()).toEqual('my-default-value');
869+
expect(echoResource.error()).toEqual(new Error('Something went wrong....'));
870+
871+
await backend.flush();
872+
TestBed.tick();
873+
874+
expect(echoResource.status()).toBe('resolved');
875+
expect(echoResource.isLoading()).toBeFalse();
876+
expect(echoResource.hasValue()).toBeTrue();
877+
expect(echoResource.value()).toEqual({});
878+
expect(echoResource.error()).toEqual(undefined);
879+
});
838880
});
839881

840882
function flushMicrotasks(): Promise<void> {

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/angular/angular/commit/6e79eaf7399170611bcbefda0082947b629f2693

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy