Throwing Exception In Dependancy/Service

Hello!

I am wanting to throw a “HttpNotFoundException” inside of a service/dependancy that I have however the exception requires the request variable which is the type: “ServerRequestInterface”. My services don’t know about the request variable so this is not possible.

I am mainly using the slim framework template. I am using an Endpoint -> Service model so that functions inside of the services can be used in multiple different endpoints.

I know that I could just make my own exceptions but I wanted to first check if there was a way to either access the request variable from somewhere else OR there was another exception that already exists.

Thanks,
Joe

The Domain layer of ADR or Model layer of MVC 2 should work completely separated from the HTTP context. Trowing a HTTP specific exception within the Domain layer would completly violate the separation of concerns (SoC) design principle.

Hey Odan,

Thanks for the reply.

I completely get this and understand why it would be separate from the HTTP context. How would you go about throwing separate errors inside of a service function? Would you create a different exception for each error that could be found? Or just make the function return null or false?

Thanks,
Joe

I would trow only domain specific exceptions, for example the generic \DomainException when something does not exist in the database or a ValidationException when the input validation fails.

Then I add a Middleware that catches only a specific set of Exceptions and transform it into a specific (JSON) response. So I can return different responses depending on the exception type. At the same time the Domain is free of any HTTP specific concern.