Skip to content

Commit

Permalink
Merge branch '1.x' into 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
crynobone committed Jun 3, 2020
2 parents 52f3bab + 2f3b428 commit f5a5445
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-1.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Released: 2020-05-01
* Added `Minions\Client\Minion::enabled()` method.
* Added `Minions\Testing\MakesRpcRequests::sendRpc()` method.
* Added `Minions\Testing\TestResponse::assertStatus()` method.

## 1.8.0

Released: 2020-04-20
Expand Down
8 changes: 8 additions & 0 deletions src/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ public function getRpcVersion(): string
return $this->content['jsonrpc'];
}

/**
* Get RPC error exception name.
*/
public function getRpcError(): ?string
{
return $this->content['error']['exception'] ?? null;
}

/**
* Get RPC error code.
*/
Expand Down
30 changes: 30 additions & 0 deletions src/Exceptions/RequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ public function getRequestMethod(): string
return $this->requestMethod;
}

/**
* Get RPC error exception name.
*
* @return mixed
*/
public function getRpcError()
{
return $this->response->getRpcError();
}

/**
* Get RPC error code.
*
* @return mixed
*/
public function getRpcErrorCode()
{
return $this->response->getRpcErrorCode();
}

/**
* Get RPC error message.
*
* @return mixed
*/
public function getRpcErrorMessage()
{
return $this->response->getRpcErrorMessage();
}

/**
* Get RPC error data.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Http/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ protected function handleModelNotFoundException(ModelNotFoundException $exceptio
$error = [
'code' => -32602,
'message' => $exception->getMessage(),
'exception' => \get_class($exception),
'data' => [
'model' => $exception->getModel(),
'ids' => $exception->getIds(),
Expand All @@ -78,6 +79,7 @@ protected function handleValidationException(ValidationException $exception): Re
$error = [
'code' => -32602,
'message' => $exception->getMessage(),
'exception' => \get_class($exception),
'data' => $exception->errors() ?? null,
];

Expand All @@ -103,6 +105,7 @@ protected function handleGenericException($exception): Reply
'error' => [
'code' => -32603,
'message' => \get_class($exception).' - '.$exception->getMessage(),
'exception' => \get_class($exception),
],
]));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Http/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ public function it_cant_dispatch_the_request_when_project_with_failed_validation
$reply = $this->app['minions.router']->handle($request);

$this->assertInstanceOf('Minions\Http\Reply', $reply);
$this->assertSame('{"jsonrpc":"2.0","id":null,"error":{"code":-32602,"message":"The given data was invalid.","data":{"email":["The email must be a valid email address."]}}}', $reply->body());
$this->assertSame('{"jsonrpc":"2.0","id":null,"error":{"code":-32602,"message":"The given data was invalid.","exception":"Illuminate\\\Validation\\\ValidationException","data":{"email":["The email must be a valid email address."]}}}', $reply->body());
}

/** @test */
Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/Client/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,28 @@ public function it_can_received_failed_response()
$this->assertNull($response->getRpcErrorData());
}

/** @test */
public function it_can_received_failed_response_for_model_not_found()
{
$psr7Response = m::mock(ResponseContract::class);

$psr7Response->shouldReceive('getStatusCode')->andReturn(200)
->shouldReceive('getBody')->andReturn('{"jsonrpc":"2.0","id":null,"error":{"code":-32602,"message":"No query results for model [User] 2","exception":"Illuminate\\\Database\\\Eloquent\\\ModelNotFoundException","data":{"model":"User","ids":[2]}}}');

$response = new Response($psr7Response);

$this->assertNull($response->getRpcId());
$this->assertNull($response->getRpcResult());
$this->assertSame('2.0', $response->getRpcVersion());
$this->assertSame('Illuminate\Database\Eloquent\ModelNotFoundException', $response->getRpcError());
$this->assertSame(-32602, $response->getRpcErrorCode());
$this->assertSame('No query results for model [User] 2', $response->getRpcErrorMessage());
$this->assertSame([
'model' => 'User',
'ids' => [2],
], $response->getRpcErrorData());
}

/** @test */
public function it_can_throw_client_has_error_exception()
{
Expand Down
10 changes: 8 additions & 2 deletions tests/Unit/Exceptions/RequestExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ public function it_can_be_thrown()
{
$response = m::mock(ResponseInterface::class);

$response->shouldReceive('getRpcErrorData')->andReturn(['project' => 'foo']);
$response->shouldReceive('getRpcError')->andReturn('Illuminate\Validation\ValidationException')
->shouldReceive('getRpcErrorCode')->andReturn(-32602)
->shouldReceive('getRpcErrorMessage')->andReturn('The given data was invalid.')
->shouldReceive('getRpcErrorData')->andReturn(["Password is required"]);

$exception = new RequestException(
'Unable to find project', -32600, $response, 'math.add'
Expand All @@ -33,6 +36,9 @@ public function it_can_be_thrown()
$this->assertSame(-32600, $exception->getCode());
$this->assertSame('math.add', $exception->getRequestMethod());

$this->assertSame(['project' => 'foo'], $exception->getRpcErrorData());
$this->assertSame('Illuminate\Validation\ValidationException', $exception->getRpcError());
$this->assertSame(-32602, $exception->getRpcErrorCode());
$this->assertSame('The given data was invalid.', $exception->getRpcErrorMessage());
$this->assertSame(["Password is required"], $exception->getRpcErrorData());
}
}
6 changes: 3 additions & 3 deletions tests/Unit/Http/ExceptionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function it_can_handle_model_not_found_exception()
$reply = $handler->handle($exception);

$this->assertSame(
'{"jsonrpc":"2.0","id":null,"error":{"code":-32602,"message":"No query results for model [User] 2","data":{"model":"User","ids":[2]}}}',
'{"jsonrpc":"2.0","id":null,"error":{"code":-32602,"message":"No query results for model [User] 2","exception":"Illuminate\\\Database\\\Eloquent\\\ModelNotFoundException","data":{"model":"User","ids":[2]}}}',
$reply->body()
);
}
Expand All @@ -66,7 +66,7 @@ public function it_can_handle_validation_exception()
$reply = $handler->handle(new ValidationException($validator));

$this->assertSame(
'{"jsonrpc":"2.0","id":null,"error":{"code":-32602,"message":"The given data was invalid.","data":["Password is required"]}}',
'{"jsonrpc":"2.0","id":null,"error":{"code":-32602,"message":"The given data was invalid.","exception":"Illuminate\\\Validation\\\ValidationException","data":["Password is required"]}}',
$reply->body()
);
}
Expand All @@ -89,7 +89,7 @@ public function it_can_handle_generic_exception()
$reply = $handler->handle($exception);

$this->assertSame(
'{"jsonrpc":"2.0","id":null,"error":{"code":-32603,"message":"Illuminate\\\Database\\\QueryException - (SQL: SELECT * FROM `users` WHERE email=crynobone@katsana.com)"}}',
'{"jsonrpc":"2.0","id":null,"error":{"code":-32603,"message":"Illuminate\\\Database\\\QueryException - (SQL: SELECT * FROM `users` WHERE email=crynobone@katsana.com)","exception":"Illuminate\\\Database\\\QueryException"}}',
$reply->body()
);

Expand Down

0 comments on commit f5a5445

Please sign in to comment.