I'm new to the SOLID principles. I have this scenario:
class ErrorClass {
constructor(name: string, description: string) {
//
}
}
class Class1 {
public someMethod() {
try {
// good scenario
} catch (error) {
throw new ErrorClass("Error Name","Error Description");
}
}
}
Is that wrong? Am I allowed to call ErrorClass
directly from Class1
or am I supposed to inject ErrorClass
into Class1
as a dependency? I'm worried that my Class1
constructor can get bloated if I have to inject different error classes along with other classes it might also depend on?
I would suggest you read upon Cross-cutting concern. Here are a few links:
Please note, that in most cases they give example of a Logger class as a Cross-cutting concern - a logger is something that every class needs, and according to the SOLID principles it should be injected as a ctor parameter to every class, right? Wrong. Logger class is an exception to the SOLID principles because it is a Cross-cutting concern.
And in your case, your ErrorClass
is also a Cross-cutting concern. Therefore, it should be a global static and not injected as a parameter to ctor.