0

On laravel 9 site in my custom class method raise custom error with error message

<?php

    class CustomClass
    {
    
        use Illuminate\Database\Eloquent\ModelNotFoundException;
        use Illuminate\Http\Exceptions\HttpResponseException;
        ...
        public function method(): array
        {
    
        if(false) {
            throw new HttpResponseException(response()->json([
                'message' => $validated['message'],
            ], 422));
        }

but I failed to catch this error in control where methods raised :

{
    try {
        $imageUploadSuccess = $customClass->method(
            ...
        ); // This method can raise different types of exception
    }
    catch (ModelNotFoundException $e) { // exception with invalid id is catched - THIS PART WORKS OK 
        return response()->json([
            'message' => 'Item  not found',
        ], 404);
    }
    catch (HttpResponseException $e) { //  I catch HttpResponseException with is generated in method
        // None of these 2 methods returns custom error message in method :
        \Log::info($e->getMessage()); // empty value - I NEED TO GET THIS MESSAGE TEXT
        \Log::info($e->getCode());    // 0 value
        \Log::info($e->getTraceAsString());
        return response()->json([
            'message' => $e->getMessage(),
        ], 422);
    }
    return response()->json(['success' => true],  400);

Have I to use other class, not HttpResponseException ? I which way ?

Thanks in advance!

Petro Gromovo
  • 1,755
  • 5
  • 33
  • 91

1 Answers1

1

You’re not throwing your custom exception so it’s never raised so it won’t be catchable.

Replace where you throw the HttpResponseException with your Exception.

<?php

class CustomClass
{
    public function method()
    {
        $isValid = false;
        if(!$isValid) {
            throw new SomeCustomException(response()->json([
                'message' => $validated['message'],
            ], 422));
        }
    }
}

You would then use do something like:

$customClass = new CustomClass();
$customClass->method();

Note how I have defined a variable $isValid and how I check it for a true or false value in the if statement. This is because if checks for truthy values and false will never be `true so your custom exception would never be thrown.

if (false) {
    // code in here will never execute
}
Peppermintology
  • 9,343
  • 3
  • 27
  • 51
  • Do you mean " with your Exception" to create custom exception ? – Petro Gromovo Oct 15 '22 at 08:32
  • 1
    @PetroGromovo I have literally shown you what I mean – Peppermintology Oct 15 '22 at 08:34
  • Sorry, I did not understand you. I show piece of code with ModelNotFoundException - to catch error of invalid item id. But I need to get custom data inside of catch (HttpResponseException $e) {... – Petro Gromovo Oct 15 '22 at 10:13
  • @PetroGromovo I have updated my answer to provide more clarity. – Peppermintology Oct 15 '22 at 12:00
  • What is in your responce ModelNotFoundException ? Custom exception? Actually ModelNotFoundException is laravel standart exception. I put it into my code to show that there several exceptions for method calling. I tried to use HttpResponseException(I found ref to it in the net...). Sorry, if I pure put my question. – Petro Gromovo Oct 15 '22 at 12:06
  • @PetroGromovo Can you update your question please to provide your actual custom exception and how/where you intend to use it. It appears you have omitted crucial information from your question which is making answering it more complicated. – Peppermintology Oct 15 '22 at 12:08
  • 1
    @PetroGromovo Do you actually have a custom exception, or do you just mean you want to customise the message of `HttpResponseException`? – Peppermintology Oct 15 '22 at 13:47
  • 1) I tried to use HttpResponseException (I found ref to it in net), but failed with error message. 2) Maybe I have to create a custom exception and use it? In which way ? – Petro Gromovo Oct 15 '22 at 14:40
  • 1
    @PetroGromovo You're not answering the question I have asked. What are you trying to do? Are you trying to use a custom exception, or are you trying to customise the `HttpResponseException` message? – Peppermintology Oct 15 '22 at 14:43
  • I try with raised exception in method to pass error message to controller from where method is called. I tried to customise and use HttpResponseException. I do not know which way is valid... – Petro Gromovo Oct 15 '22 at 14:49
  • 1
    @PetroGromovo OK, so if you're code is **EXACTLY** as it is in your question the problem is your `if(false)` statement. I have already explained what is wrong with this in my answer. – Peppermintology Oct 15 '22 at 15:19