Angular how to avoid that errors break valueChanges
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
In my angular 6 application, I have a problem with some rxjs operators.
I have a search field and I need to call my service (service do GET request) when the user is typing in the field, it is working if the service doesn't have any errors like 500.
But a request with 500 error break the chain in the value change, probably I don't return the correct value from the service...
this is the search field in the component:
this.manufacturer.valueChanges
.pipe(
filter((value: string) => (value ? value.length > 2 : false)),
tap(() => (this.loadingAutocomplete = true)),
debounceTime(500),
switchMap(val => this.frameService.getItems(val, "manufacturerAutocomplete")),
tap(() => (this.loadingAutocomplete = false)),
catchError(err =>
return of())
)
.subscribe(
manufacturers =>
this.matchingManufacturers.next(manufacturers);
,
err =>
this.loadingAutocomplete = false;
this.matchingManufacturers.next();
);
And this is the service:
getItems(value: string, endPoint: string)
return this.http.get(
this.endPointUrlService.cutLinks(this.endPointUrlService.map.get("frames")) + "/search/" + endPoint
).pipe(
map((res: string) =>
if (res)
return res;
else
return ;
)
);
First time user types something it works, if I have an erros the valueChanges is not invoked again.
I need to handle the errors in the component, not in the service.
add a comment |
In my angular 6 application, I have a problem with some rxjs operators.
I have a search field and I need to call my service (service do GET request) when the user is typing in the field, it is working if the service doesn't have any errors like 500.
But a request with 500 error break the chain in the value change, probably I don't return the correct value from the service...
this is the search field in the component:
this.manufacturer.valueChanges
.pipe(
filter((value: string) => (value ? value.length > 2 : false)),
tap(() => (this.loadingAutocomplete = true)),
debounceTime(500),
switchMap(val => this.frameService.getItems(val, "manufacturerAutocomplete")),
tap(() => (this.loadingAutocomplete = false)),
catchError(err =>
return of())
)
.subscribe(
manufacturers =>
this.matchingManufacturers.next(manufacturers);
,
err =>
this.loadingAutocomplete = false;
this.matchingManufacturers.next();
);
And this is the service:
getItems(value: string, endPoint: string)
return this.http.get(
this.endPointUrlService.cutLinks(this.endPointUrlService.map.get("frames")) + "/search/" + endPoint
).pipe(
map((res: string) =>
if (res)
return res;
else
return ;
)
);
First time user types something it works, if I have an erros the valueChanges is not invoked again.
I need to handle the errors in the component, not in the service.
add a comment |
In my angular 6 application, I have a problem with some rxjs operators.
I have a search field and I need to call my service (service do GET request) when the user is typing in the field, it is working if the service doesn't have any errors like 500.
But a request with 500 error break the chain in the value change, probably I don't return the correct value from the service...
this is the search field in the component:
this.manufacturer.valueChanges
.pipe(
filter((value: string) => (value ? value.length > 2 : false)),
tap(() => (this.loadingAutocomplete = true)),
debounceTime(500),
switchMap(val => this.frameService.getItems(val, "manufacturerAutocomplete")),
tap(() => (this.loadingAutocomplete = false)),
catchError(err =>
return of())
)
.subscribe(
manufacturers =>
this.matchingManufacturers.next(manufacturers);
,
err =>
this.loadingAutocomplete = false;
this.matchingManufacturers.next();
);
And this is the service:
getItems(value: string, endPoint: string)
return this.http.get(
this.endPointUrlService.cutLinks(this.endPointUrlService.map.get("frames")) + "/search/" + endPoint
).pipe(
map((res: string) =>
if (res)
return res;
else
return ;
)
);
First time user types something it works, if I have an erros the valueChanges is not invoked again.
I need to handle the errors in the component, not in the service.
In my angular 6 application, I have a problem with some rxjs operators.
I have a search field and I need to call my service (service do GET request) when the user is typing in the field, it is working if the service doesn't have any errors like 500.
But a request with 500 error break the chain in the value change, probably I don't return the correct value from the service...
this is the search field in the component:
this.manufacturer.valueChanges
.pipe(
filter((value: string) => (value ? value.length > 2 : false)),
tap(() => (this.loadingAutocomplete = true)),
debounceTime(500),
switchMap(val => this.frameService.getItems(val, "manufacturerAutocomplete")),
tap(() => (this.loadingAutocomplete = false)),
catchError(err =>
return of())
)
.subscribe(
manufacturers =>
this.matchingManufacturers.next(manufacturers);
,
err =>
this.loadingAutocomplete = false;
this.matchingManufacturers.next();
);
And this is the service:
getItems(value: string, endPoint: string)
return this.http.get(
this.endPointUrlService.cutLinks(this.endPointUrlService.map.get("frames")) + "/search/" + endPoint
).pipe(
map((res: string) =>
if (res)
return res;
else
return ;
)
);
First time user types something it works, if I have an erros the valueChanges is not invoked again.
I need to handle the errors in the component, not in the service.
edited Nov 15 '18 at 14:05
SiddAjmera
16.2k31240
16.2k31240
asked Nov 15 '18 at 13:51
AlessandroAlessandro
95432154
95432154
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
The observable that results from your rxjs statement on the valueChanges property is completed, and that is why it doesnt react anymore.
Important: an observable that errors, will be completed, and there is nothing that you can do to alter that behavior.
What you need to do is prevent the error from your endpoint service from flowing down into the switchMap statement, and further into the chain. If it flows into the chain, that chain will be be completed. To prevent the error from flowing down, you need to return empty() when your api call throws an error.
getItems(value: string, endPoint: string)
return this.http.get(this.endPointUrlService.cutLinks(this.endPointUrlService.map.get("frames")) + "/search/" + endPoint).pipe(
map((res: string) =>
if (res)
return res;
else
return ;
),
catchError(err =>
// report error to user
console.log(err);
// Important: stop observable from emitting anything
return empty();
)
);
More info on the subject:
http://www.syntaxsuccess.com/viewarticle/error-handling-in-rxjs
add a comment |
Catch error earlier - in the service layer, like this:
getItems(value: string, endPoint: string)
return this.http.get(this.endPointUrlService.cutLinks(this.endPointUrlService.map.get("frames")) + "/search/" + endPoint).pipe(
map((res: string) =>
if (res)
return res;
else
return ;
),
catchError(err => return of())
);
I need to catch the error in the component If I returnI don't know if it is an error or if the list is really empty
– Alessandro
Nov 15 '18 at 13:59
So handle this behaviour in desired way, but earlier. If you don't catch error before, you get it inswitchMap.
– magos
Nov 15 '18 at 14:03
add a comment |
Probably this method solve my problem, this is the component, and the service doesn't catch anything:
this.manufacturer.valueChanges
.pipe(
filter((value: string) => (value ? value.length > 0 : false)),
tap(() => (this.loadingAutocomplete = true)),
debounceTime(500),
switchMap(val =>
this.frameService.getItems(val, "manufacturerAutocomplete").pipe(
catchError(err =>
handleError(err);
this.loadingAutocomplete = false;
return ;
)
)
),
tap(() => (this.loadingAutocomplete = false))
)
.subscribe(
manufacturers =>
this.matchingManufacturers.next(manufacturers);
,
err =>
this.loadingAutocomplete = false;
this.matchingManufacturers.next();
);
1
So you did catch error earlier as recommended. Good job.
– magos
Nov 15 '18 at 14:35
1
Me, because publishing the answer inspired by people who devoted their time to help you is denial of idea of this service.
– magos
Nov 15 '18 at 15:02
@magos you didn't post the correct code, I say" I need to catch error in component" and you write catchError in service...
– Alessandro
Nov 16 '18 at 14:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320957%2fangular-how-to-avoid-that-errors-break-valuechanges%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The observable that results from your rxjs statement on the valueChanges property is completed, and that is why it doesnt react anymore.
Important: an observable that errors, will be completed, and there is nothing that you can do to alter that behavior.
What you need to do is prevent the error from your endpoint service from flowing down into the switchMap statement, and further into the chain. If it flows into the chain, that chain will be be completed. To prevent the error from flowing down, you need to return empty() when your api call throws an error.
getItems(value: string, endPoint: string)
return this.http.get(this.endPointUrlService.cutLinks(this.endPointUrlService.map.get("frames")) + "/search/" + endPoint).pipe(
map((res: string) =>
if (res)
return res;
else
return ;
),
catchError(err =>
// report error to user
console.log(err);
// Important: stop observable from emitting anything
return empty();
)
);
More info on the subject:
http://www.syntaxsuccess.com/viewarticle/error-handling-in-rxjs
add a comment |
The observable that results from your rxjs statement on the valueChanges property is completed, and that is why it doesnt react anymore.
Important: an observable that errors, will be completed, and there is nothing that you can do to alter that behavior.
What you need to do is prevent the error from your endpoint service from flowing down into the switchMap statement, and further into the chain. If it flows into the chain, that chain will be be completed. To prevent the error from flowing down, you need to return empty() when your api call throws an error.
getItems(value: string, endPoint: string)
return this.http.get(this.endPointUrlService.cutLinks(this.endPointUrlService.map.get("frames")) + "/search/" + endPoint).pipe(
map((res: string) =>
if (res)
return res;
else
return ;
),
catchError(err =>
// report error to user
console.log(err);
// Important: stop observable from emitting anything
return empty();
)
);
More info on the subject:
http://www.syntaxsuccess.com/viewarticle/error-handling-in-rxjs
add a comment |
The observable that results from your rxjs statement on the valueChanges property is completed, and that is why it doesnt react anymore.
Important: an observable that errors, will be completed, and there is nothing that you can do to alter that behavior.
What you need to do is prevent the error from your endpoint service from flowing down into the switchMap statement, and further into the chain. If it flows into the chain, that chain will be be completed. To prevent the error from flowing down, you need to return empty() when your api call throws an error.
getItems(value: string, endPoint: string)
return this.http.get(this.endPointUrlService.cutLinks(this.endPointUrlService.map.get("frames")) + "/search/" + endPoint).pipe(
map((res: string) =>
if (res)
return res;
else
return ;
),
catchError(err =>
// report error to user
console.log(err);
// Important: stop observable from emitting anything
return empty();
)
);
More info on the subject:
http://www.syntaxsuccess.com/viewarticle/error-handling-in-rxjs
The observable that results from your rxjs statement on the valueChanges property is completed, and that is why it doesnt react anymore.
Important: an observable that errors, will be completed, and there is nothing that you can do to alter that behavior.
What you need to do is prevent the error from your endpoint service from flowing down into the switchMap statement, and further into the chain. If it flows into the chain, that chain will be be completed. To prevent the error from flowing down, you need to return empty() when your api call throws an error.
getItems(value: string, endPoint: string)
return this.http.get(this.endPointUrlService.cutLinks(this.endPointUrlService.map.get("frames")) + "/search/" + endPoint).pipe(
map((res: string) =>
if (res)
return res;
else
return ;
),
catchError(err =>
// report error to user
console.log(err);
// Important: stop observable from emitting anything
return empty();
)
);
More info on the subject:
http://www.syntaxsuccess.com/viewarticle/error-handling-in-rxjs
edited Nov 15 '18 at 14:17
answered Nov 15 '18 at 14:12
DavyDavy
2,80641528
2,80641528
add a comment |
add a comment |
Catch error earlier - in the service layer, like this:
getItems(value: string, endPoint: string)
return this.http.get(this.endPointUrlService.cutLinks(this.endPointUrlService.map.get("frames")) + "/search/" + endPoint).pipe(
map((res: string) =>
if (res)
return res;
else
return ;
),
catchError(err => return of())
);
I need to catch the error in the component If I returnI don't know if it is an error or if the list is really empty
– Alessandro
Nov 15 '18 at 13:59
So handle this behaviour in desired way, but earlier. If you don't catch error before, you get it inswitchMap.
– magos
Nov 15 '18 at 14:03
add a comment |
Catch error earlier - in the service layer, like this:
getItems(value: string, endPoint: string)
return this.http.get(this.endPointUrlService.cutLinks(this.endPointUrlService.map.get("frames")) + "/search/" + endPoint).pipe(
map((res: string) =>
if (res)
return res;
else
return ;
),
catchError(err => return of())
);
I need to catch the error in the component If I returnI don't know if it is an error or if the list is really empty
– Alessandro
Nov 15 '18 at 13:59
So handle this behaviour in desired way, but earlier. If you don't catch error before, you get it inswitchMap.
– magos
Nov 15 '18 at 14:03
add a comment |
Catch error earlier - in the service layer, like this:
getItems(value: string, endPoint: string)
return this.http.get(this.endPointUrlService.cutLinks(this.endPointUrlService.map.get("frames")) + "/search/" + endPoint).pipe(
map((res: string) =>
if (res)
return res;
else
return ;
),
catchError(err => return of())
);
Catch error earlier - in the service layer, like this:
getItems(value: string, endPoint: string)
return this.http.get(this.endPointUrlService.cutLinks(this.endPointUrlService.map.get("frames")) + "/search/" + endPoint).pipe(
map((res: string) =>
if (res)
return res;
else
return ;
),
catchError(err => return of())
);
answered Nov 15 '18 at 13:56
magosmagos
1,81831939
1,81831939
I need to catch the error in the component If I returnI don't know if it is an error or if the list is really empty
– Alessandro
Nov 15 '18 at 13:59
So handle this behaviour in desired way, but earlier. If you don't catch error before, you get it inswitchMap.
– magos
Nov 15 '18 at 14:03
add a comment |
I need to catch the error in the component If I returnI don't know if it is an error or if the list is really empty
– Alessandro
Nov 15 '18 at 13:59
So handle this behaviour in desired way, but earlier. If you don't catch error before, you get it inswitchMap.
– magos
Nov 15 '18 at 14:03
I need to catch the error in the component If I return
I don't know if it is an error or if the list is really empty– Alessandro
Nov 15 '18 at 13:59
I need to catch the error in the component If I return
I don't know if it is an error or if the list is really empty– Alessandro
Nov 15 '18 at 13:59
So handle this behaviour in desired way, but earlier. If you don't catch error before, you get it in
switchMap.– magos
Nov 15 '18 at 14:03
So handle this behaviour in desired way, but earlier. If you don't catch error before, you get it in
switchMap.– magos
Nov 15 '18 at 14:03
add a comment |
Probably this method solve my problem, this is the component, and the service doesn't catch anything:
this.manufacturer.valueChanges
.pipe(
filter((value: string) => (value ? value.length > 0 : false)),
tap(() => (this.loadingAutocomplete = true)),
debounceTime(500),
switchMap(val =>
this.frameService.getItems(val, "manufacturerAutocomplete").pipe(
catchError(err =>
handleError(err);
this.loadingAutocomplete = false;
return ;
)
)
),
tap(() => (this.loadingAutocomplete = false))
)
.subscribe(
manufacturers =>
this.matchingManufacturers.next(manufacturers);
,
err =>
this.loadingAutocomplete = false;
this.matchingManufacturers.next();
);
1
So you did catch error earlier as recommended. Good job.
– magos
Nov 15 '18 at 14:35
1
Me, because publishing the answer inspired by people who devoted their time to help you is denial of idea of this service.
– magos
Nov 15 '18 at 15:02
@magos you didn't post the correct code, I say" I need to catch error in component" and you write catchError in service...
– Alessandro
Nov 16 '18 at 14:13
add a comment |
Probably this method solve my problem, this is the component, and the service doesn't catch anything:
this.manufacturer.valueChanges
.pipe(
filter((value: string) => (value ? value.length > 0 : false)),
tap(() => (this.loadingAutocomplete = true)),
debounceTime(500),
switchMap(val =>
this.frameService.getItems(val, "manufacturerAutocomplete").pipe(
catchError(err =>
handleError(err);
this.loadingAutocomplete = false;
return ;
)
)
),
tap(() => (this.loadingAutocomplete = false))
)
.subscribe(
manufacturers =>
this.matchingManufacturers.next(manufacturers);
,
err =>
this.loadingAutocomplete = false;
this.matchingManufacturers.next();
);
1
So you did catch error earlier as recommended. Good job.
– magos
Nov 15 '18 at 14:35
1
Me, because publishing the answer inspired by people who devoted their time to help you is denial of idea of this service.
– magos
Nov 15 '18 at 15:02
@magos you didn't post the correct code, I say" I need to catch error in component" and you write catchError in service...
– Alessandro
Nov 16 '18 at 14:13
add a comment |
Probably this method solve my problem, this is the component, and the service doesn't catch anything:
this.manufacturer.valueChanges
.pipe(
filter((value: string) => (value ? value.length > 0 : false)),
tap(() => (this.loadingAutocomplete = true)),
debounceTime(500),
switchMap(val =>
this.frameService.getItems(val, "manufacturerAutocomplete").pipe(
catchError(err =>
handleError(err);
this.loadingAutocomplete = false;
return ;
)
)
),
tap(() => (this.loadingAutocomplete = false))
)
.subscribe(
manufacturers =>
this.matchingManufacturers.next(manufacturers);
,
err =>
this.loadingAutocomplete = false;
this.matchingManufacturers.next();
);
Probably this method solve my problem, this is the component, and the service doesn't catch anything:
this.manufacturer.valueChanges
.pipe(
filter((value: string) => (value ? value.length > 0 : false)),
tap(() => (this.loadingAutocomplete = true)),
debounceTime(500),
switchMap(val =>
this.frameService.getItems(val, "manufacturerAutocomplete").pipe(
catchError(err =>
handleError(err);
this.loadingAutocomplete = false;
return ;
)
)
),
tap(() => (this.loadingAutocomplete = false))
)
.subscribe(
manufacturers =>
this.matchingManufacturers.next(manufacturers);
,
err =>
this.loadingAutocomplete = false;
this.matchingManufacturers.next();
);
answered Nov 15 '18 at 14:22
AlessandroAlessandro
95432154
95432154
1
So you did catch error earlier as recommended. Good job.
– magos
Nov 15 '18 at 14:35
1
Me, because publishing the answer inspired by people who devoted their time to help you is denial of idea of this service.
– magos
Nov 15 '18 at 15:02
@magos you didn't post the correct code, I say" I need to catch error in component" and you write catchError in service...
– Alessandro
Nov 16 '18 at 14:13
add a comment |
1
So you did catch error earlier as recommended. Good job.
– magos
Nov 15 '18 at 14:35
1
Me, because publishing the answer inspired by people who devoted their time to help you is denial of idea of this service.
– magos
Nov 15 '18 at 15:02
@magos you didn't post the correct code, I say" I need to catch error in component" and you write catchError in service...
– Alessandro
Nov 16 '18 at 14:13
1
1
So you did catch error earlier as recommended. Good job.
– magos
Nov 15 '18 at 14:35
So you did catch error earlier as recommended. Good job.
– magos
Nov 15 '18 at 14:35
1
1
Me, because publishing the answer inspired by people who devoted their time to help you is denial of idea of this service.
– magos
Nov 15 '18 at 15:02
Me, because publishing the answer inspired by people who devoted their time to help you is denial of idea of this service.
– magos
Nov 15 '18 at 15:02
@magos you didn't post the correct code, I say" I need to catch error in component" and you write catchError in service...
– Alessandro
Nov 16 '18 at 14:13
@magos you didn't post the correct code, I say" I need to catch error in component" and you write catchError in service...
– Alessandro
Nov 16 '18 at 14:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320957%2fangular-how-to-avoid-that-errors-break-valuechanges%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown