Skip to content

Commit 4589515

Browse files
committed
Implemented Exceptions
1 parent 39f7043 commit 4589515

File tree

9 files changed

+184
-0
lines changed

9 files changed

+184
-0
lines changed

src/DTO/Exception.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodebarAg\MFiles\DTO;
6+
7+
use Illuminate\Support\Arr;
8+
9+
final class Exception
10+
{
11+
public function __construct(
12+
public readonly string $name,
13+
public readonly string $message,
14+
public readonly ?InnerException $innerException,
15+
) {}
16+
17+
public static function fromArray(array $data): self
18+
{
19+
$innerExceptionData = Arr::get($data, 'InnerException');
20+
$innerException = $innerExceptionData ? InnerException::fromArray($innerExceptionData) : null;
21+
22+
return new self(
23+
name: Arr::get($data, 'Name', ''),
24+
message: Arr::get($data, 'Message', ''),
25+
innerException: $innerException,
26+
);
27+
}
28+
29+
public function toArray(): array
30+
{
31+
return [
32+
'name' => $this->name,
33+
'message' => $this->message,
34+
'innerException' => $this->innerException?->toArray(),
35+
];
36+
}
37+
}

src/DTO/InnerException.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodebarAg\MFiles\DTO;
6+
7+
use Illuminate\Support\Arr;
8+
9+
final class InnerException
10+
{
11+
public function __construct(
12+
public readonly string $name,
13+
public readonly string $message,
14+
public readonly ?string $stackText,
15+
public readonly ?string $errorCode,
16+
) {}
17+
18+
public static function fromArray(array $data): self
19+
{
20+
return new self(
21+
name: Arr::get($data, 'Name', ''),
22+
message: Arr::get($data, 'Message', ''),
23+
stackText: Arr::get($data, 'StackText'),
24+
errorCode: Arr::get($data, 'ErrorCode'),
25+
);
26+
}
27+
28+
public function toArray(): array
29+
{
30+
return [
31+
'name' => $this->name,
32+
'message' => $this->message,
33+
'stackText' => $this->stackText,
34+
'errorCode' => $this->errorCode,
35+
];
36+
}
37+
}

src/DTO/MFilesError.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodebarAg\MFiles\DTO;
6+
7+
use Illuminate\Support\Arr;
8+
9+
final class MFilesError
10+
{
11+
public function __construct(
12+
public readonly string $errorCode,
13+
public readonly int $status,
14+
public readonly string $url,
15+
public readonly string $method,
16+
public readonly ?Exception $exception,
17+
public readonly ?string $stack,
18+
public readonly string $message,
19+
public readonly bool $isLoggedToVault,
20+
public readonly bool $isLoggedToApplication,
21+
public readonly string $exceptionName,
22+
) {}
23+
24+
public static function fromArray(array $data): self
25+
{
26+
$exceptionData = Arr::get($data, 'Exception');
27+
$exception = $exceptionData ? Exception::fromArray($exceptionData) : null;
28+
29+
return new self(
30+
errorCode: Arr::get($data, 'ErrorCode', ''),
31+
status: Arr::get($data, 'Status', 0),
32+
url: Arr::get($data, 'URL', ''),
33+
method: Arr::get($data, 'Method', ''),
34+
exception: $exception,
35+
stack: Arr::get($data, 'Stack'),
36+
message: Arr::get($data, 'Message', ''),
37+
isLoggedToVault: Arr::get($data, 'IsLoggedToVault', false),
38+
isLoggedToApplication: Arr::get($data, 'IsLoggedToApplication', false),
39+
exceptionName: Arr::get($data, 'ExceptionName', ''),
40+
);
41+
}
42+
43+
public function toArray(): array
44+
{
45+
return [
46+
'errorCode' => $this->errorCode,
47+
'status' => $this->status,
48+
'url' => $this->url,
49+
'method' => $this->method,
50+
'exception' => $this->exception?->toArray(),
51+
'stack' => $this->stack,
52+
'message' => $this->message,
53+
'isLoggedToVault' => $this->isLoggedToVault,
54+
'isLoggedToApplication' => $this->isLoggedToApplication,
55+
'exceptionName' => $this->exceptionName,
56+
];
57+
}
58+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodebarAg\MFiles\Exceptions;
6+
7+
use CodebarAg\MFiles\DTO\MFilesError;
8+
use Exception as BaseException;
9+
10+
final class MFilesErrorException extends BaseException
11+
{
12+
public function __construct(
13+
public readonly MFilesError $error
14+
) {
15+
parent::__construct($error->message, $error->status);
16+
}
17+
}

src/Responses/DownloadFileResponse.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
namespace CodebarAg\MFiles\Responses;
66

77
use CodebarAg\MFiles\DTO\DownloadedFile;
8+
use CodebarAg\MFiles\Exceptions\MFilesErrorException;
89
use Saloon\Http\Response;
910

1011
final class DownloadFileResponse
1112
{
1213
public static function createDtoFromResponse(Response $response): DownloadedFile
1314
{
15+
if (! $response->successful()) {
16+
throw new MFilesErrorException(ErrorResponse::createDtoFromResponse($response));
17+
}
1418
$headers = $response->headers();
1519
$fileContentType = $headers->get('Content-Type');
1620
$fileSize = (int) $headers->get('Content-Length', 0);

src/Responses/ErrorResponse.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CodebarAg\MFiles\Responses;
6+
7+
use CodebarAg\MFiles\DTO\MFilesError;
8+
use Saloon\Http\Response;
9+
10+
final class ErrorResponse
11+
{
12+
public static function createDtoFromResponse(Response $response): MFilesError
13+
{
14+
return MFilesError::fromArray($response->json());
15+
}
16+
}

src/Responses/LogInToVaultResponse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44

55
namespace CodebarAg\MFiles\Responses;
66

7+
use CodebarAg\MFiles\Exceptions\MFilesErrorException;
78
use Illuminate\Support\Arr;
89
use Saloon\Http\Response;
910

1011
final class LogInToVaultResponse
1112
{
1213
public static function createDtoFromResponse(Response $response): ?string
1314
{
15+
if (! $response->successful()) {
16+
throw new MFilesErrorException(ErrorResponse::createDtoFromResponse($response));
17+
}
18+
1419
return Arr::get($response->json(), 'Value');
1520
}
1621
}

src/Responses/ObjectPropertiesResponse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
namespace CodebarAg\MFiles\Responses;
66

77
use CodebarAg\MFiles\DTO\ObjectProperties;
8+
use CodebarAg\MFiles\Exceptions\MFilesErrorException;
89
use Saloon\Http\Response;
910

1011
final class ObjectPropertiesResponse
1112
{
1213
public static function createDtoFromResponse(Response $response): ObjectProperties
1314
{
15+
if (! $response->successful()) {
16+
throw new MFilesErrorException(ErrorResponse::createDtoFromResponse($response));
17+
}
18+
1419
return ObjectProperties::fromArray($response->json());
1520
}
1621
}

src/Responses/UploadFileResponse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace CodebarAg\MFiles\Responses;
66

7+
use CodebarAg\MFiles\Exceptions\MFilesErrorException;
78
use Illuminate\Support\Arr;
89
use Illuminate\Support\Str;
910
use Saloon\Http\Response;
@@ -12,6 +13,10 @@ final class UploadFileResponse
1213
{
1314
public static function createDtoFromResponse(Response $response, string $fileName): array
1415
{
16+
if (! $response->successful()) {
17+
throw new MFilesErrorException(ErrorResponse::createDtoFromResponse($response));
18+
}
19+
1520
$data = $response->json();
1621
$data = Arr::add($data, 'Title', Str::beforeLast($fileName, '.'));
1722
$data = Arr::add($data, 'Extension', Str::afterLast($fileName, '.'));

0 commit comments

Comments
 (0)