0

I have created a worker class in Laravel.

The worker class communicate to with Lumen. If there is an error in Lumen it will response back in json to Laravel.

The worker class like this:-

class Worker {

    public function put($ip, $user, $file)
    {
        try {
            $response = $this->client->post('/put', ['form_params' => ['ip' => $ip,'username' => $user, 'file'  => $file]]);

            $responseBody = (string)$response->getBody();
            // do something

        } catch (ClientException | ServerException $e) {
            return $this->handleRequestError($e);
        }
    }


    protected function handleRequestError($e)
    {
        if ($e instanceof ClientException) {
            if ($e->getCode() == 422) {
                throw new WorkerValidationException((string)$e->getResponse()->getBody());
            }
        }

        if ($e instanceof ServerException) {
            $error = json_decode($e->getResponse()->getBody(), true);

            if ($error['error_type'] == 'ftp') {
                throw new FtpException((string)$e->getResponse()->getBody());
            }

            if ($error['error_type'] == 'somethingElse') {
                throw new SomethingElseException((string)$e->getResponse()->getBody());
            }
        }


        throw new \Exception((string) $e->getResponse()->getBody());
    }
}

The handleRequestError() method read the value of $error['error_type'] and throw specific exception.

However, I want 2 or 3 error codes ($error['code']) to response back to the user with json format. What is good approach to do this?

Eg:

if (if ($error['error_type'] == 'ftp' && $error['code'] == 200) {
   response(['success' => false, 'message' => 'could not connect']);
}

I don't want to put response logic in the worker class. Do I need to do it in Exception Handler?

1
  • The worker class looks good, it should always throw exception, It should be the responsibility of caller function to decide what exception is raise or what to handle gracefully. Commented Jun 14, 2017 at 19:10

1 Answer 1

1

You could bind an error type and error code identifier to the app container and have it create the correct exception class. For example:

app()->bind('type1-error1', ExceptionClass1::class);
app()->bind('type2-error2', ExceptionClass2::class);
app()->bind('type2-error3', ExceptionClass3::class);

These could be bound early in the application life cycle such as in AppServiceProvider boot(). Then the exception handler could resolve an instance of the correct exception based on the type-error combination using:

$e = app('type1-error1');
throw $e;

The container is a powerful tool at your disposal!

Sign up to request clarification or add additional context in comments.

9 Comments

I think you misunderstood my question. I have end up doing this kopy.io/qxgfT in the Handler.php
I'm not so sure. You're doing a lot of type and string checking to get the right exception. The service container does exactly this to bind classes to an easy to use resolving mechanism. If you have the class names of the specific exceptions you are looking for resolved all you have to do is app()->make(Exception::class) and it will do all the work for you, no if or instance checks involved.
If you take a look at put method - when put method is called in the worker class if there is an error then it will call handleRequestError method in the catch block and then throw specific exception. In the Handler.php - if $error['error']['code'] == 100 is matched then it will response in json. Make sense?
yeah exactly, let the application resolve the error type, you can register aliases for each specific exception type and more or less get rid of all the instance and if checks you have. really the only thing it comes down to is how you package the response, json or not.
So you'd still deconstruct the originial exception like you do now to get the message and error code, the you'd have most likely just one check to make the determination how the reponse/throw gets handled.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.