I'm trying to create some custom exceptions for my application. This mostly means i'm extending the HttpException
class which is pretty simple.
However, as part of the exception message, I want to pass some of the configurations for the application.
The problem is that Exceptions are not part of the module or service. They're not managed classes, so I cannot use the ConfigService
as described by the NestJS documentation.
I could use the process.env.<my_config>
approach, but it seems dirty to have to use that when I'm using ConfigService
everywhere in my services, specially when i'm also using .env
files to load some other variables. My last alternative would be to use dotenv
directly in the configuration. However all of them suffer from the same: I could be missing some important data updates/added during the app bootstrapping portion.
How can I access app level configurations from outside the managed classes?
Sample of what i'm trying to do:
import { HttpException, HttpStatus } from '@nestjs/common';
export class MyCustomException extends HttpException {
constructor(message) {
const serviceName = // Get the configuration value
const configA = // Get other configuration value
const payload = {
serviceName,
configA,
message,
}
super(payload, HttpStatus.BAD_REQUEST)
}
}