0

I have some error classes defined like so;

class CustomError implements IError {...}
class UnauthorizedError extends CustomError {...}
class NotFoundError extends CustomError {...}
class BadRequestError extends CustomError {...}

and an enum Err defined as follow;

enum Err {
      unauthorized = "UnauthorizedError",
      custom = "CustomError",
      notFound = "NotFoundError",
      badRequest = "BadRequestError",
}

how can i turn Err.notFound for example into the class NotFoundError.

mod7ex
  • 874
  • 9
  • 27
  • 1
    You mean like `if (x == Err.custom) x = new CustomError()`? – Bergi Feb 13 '22 at 01:04
  • @Bergi yes almost i want to create an instance using only the string without a switch case or if statment – mod7ex Feb 13 '22 at 01:09
  • 1
    But you need some kind of conditional. What should happen if `x` is not `Err.custom`? – Bergi Feb 13 '22 at 01:10
  • @Bergi yes it will create an instance of the other class i have all those classes already defined `CustomError` is only an example) – mod7ex Feb 13 '22 at 01:11
  • You mean you have one class per enum value? You never mentioned that in the question. – Bergi Feb 13 '22 at 01:12
  • @Bergi yes exactly i'm sorry i just though it was clear – mod7ex Feb 13 '22 at 01:14
  • 1
    Nothing wrong with a `switch` statement actually. You need some way or another to list the eligible classes. – Bergi Feb 13 '22 at 01:16

0 Answers0