HTTP Error Stack Traces Losing Information When Using HttpService
Problem Description
When using the HttpService
from the @nestjs/axios
module, any exceptions that occur during Axios HTTP requests are stripped of stack traces that point to the file and line where the request was initiated. This makes debugging and logging failed requests more challenging.
Current Behavior
The HttpService
from the @nestjs/axios
module converts Promises to Observables, which seems to be the reason for the loss of stack trace information. When using the raw Axios client, the error stack trace includes a reference to the file where the request was initiated.
Example Use Case
Here's an example of how to reproduce the issue:
// app.service.ts
import { Injectable } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { firstValueFrom } from 'rxjs';
@Injectable()
export class AppService {
constructor(private readonly httpService: HttpService) {}
async onApplicationBootstrap() {
try {
await firstValueFrom(this.httpService.get('https://example.sfdsdf'));
} catch (error) {
console.error(error);
}
}
}
When running the application, the error stack trace will not include a reference to the file app.service.ts
where the request was initiated.
Raw Axios Client Comparison
Here's an example of how to use the raw Axios client to make the same request:
// app.service.ts
import { Injectable } from '@nestjs/common';
import axios from 'axios';
@Injectable()
export class AppService {
async onApplicationBootstrap() {
try {
await axios.get('https://example.sfdsdf');
} catch (error) {
console.error(error);
}
}
}
In this case, the error stack trace will include a reference to the file app.service.ts
where the request was initiated.
Minimum Reproduction Code
You can find the minimum reproduction code in the following GitHub repository:
https://github.com/sosmo/nest-axios-reproduction
Steps to Reproduce
- Clone the linked repository
- Run
npm i
- Run
npm run start
- Observe the logged results
Expected Behavior
As described elsewhere, the error stack trace should include a reference to the file where the request was initiated.
Package Version
@nestjs/axios
: 4.0.0@nestjs/core
: 11.0.1node
: 22
NestJS Version
@nestjs/core
: 11.0.1
Node.js Version
node
: 22
Operating Systems Tested
- Linux
Other
Q: What is the issue with the HttpService from the @nestjs/axios module?
A: The HttpService
from the @nestjs/axios
module converts Promises to Observables, which seems to be the reason for the loss of stack trace information. This makes debugging and logging failed requests more challenging.
Q: Why is the stack trace information lost when using the HttpService?
A: The HttpService
converts Promises to Observables, which removes the stack trace information. This is because the Observable conversion process does not preserve the original error stack trace.
Q: How can I reproduce the issue?
A: You can reproduce the issue by creating a new NestJS application and using the HttpService
to make an HTTP request. You can find the minimum reproduction code in the following GitHub repository:
https://github.com/sosmo/nest-axios-reproduction
Q: What is the expected behavior?
A: The expected behavior is that the error stack trace should include a reference to the file where the request was initiated.
Q: How can I use the raw Axios client to make the same request?
A: You can use the raw Axios client by importing it and making the request directly. Here's an example:
// app.service.ts
import { Injectable } from '@nestjs/common';
import axios from 'axios';
@Injectable()
export class AppService {
async onApplicationBootstrap() {
try {
await axios.get('https://example.sfdsdf');
} catch (error) {
console.error(error);
}
}
}
Q: What are the package versions used in this example?
A: The package versions used in this example are:
@nestjs/axios
: 4.0.0@nestjs/core
: 11.0.1node
: 22
Q: What is the NestJS version used in this example?
A: The NestJS version used in this example is @nestjs/core
: 11.0.1
Q: What is the Node.js version used in this example?
A: The Node.js version used in this example is node
: 22
Q: In which operating systems have you tested this issue?
A: This issue has been tested in Linux.
Q: Is there a workaround for this issue?
A: Yes, you can use the raw Axios client to make the request and preserve the stack trace information.
Q: Is this issue a bug or a feature?
A: This issue is a bug in the HttpService
from the @nestjs/axios
module.