![]() ![]() ![]() Phew, now that we have the validateStatus returning true, we always get an observable response, and we can start retrying our Http calls that fail. We can either add in our own functionality to determine if the status is a 404 then reject, or if it is a 400 resolve, or we can just set the function directly to null or undefined and let the function always return true (according to the Axios config docs). This is where Axios by default determines that a status >= 200 & status < 300 is an acceptable response and resolves the promise otherwise rejects. Looking at the Axios config options, there is one calls validateStatus which is usually a function that takes in a number and returns a boolean, to determine what to do with the status code. I decided to do this at the service level, but it is possible to do in the module's import using HttpModule.register() or HttpModule.registerAsync(). ConfigurationĮither in your HttpModule import in in the HttpService call, you can pass configuration options to Axios for it to know how to react. This is a problem for any sort retrying you may want to try to do, so here's how it can be fixed. However, this all only happen is Axios resolves the promise instead of rejecting it (i.e. With the response being an RxJS Observable, a lot of really cool things can happen, including response mapping using map, internal error handling with catchError and even spying on the response with tap. NestJS uses Axios as the underlying HttpService for the HttpModule, which can be injected into any service class, but wraps the response in an Observable. However, the code here should be valid and used as a tool to understand how to create retryable HTTP requests uisng NestJS's HttpServiceīy default, if Axios receives any HTTP status code that is not in the 200-300 range, it will throw an error and reject the call (via Promise.reject). This repository is archived as I do not plan to update it again. A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |