2
2
3
3
namespace SymfonyDocsBuilder \Release ;
4
4
5
+ use Symfony \Contracts \HttpClient \Exception \HttpExceptionInterface ;
5
6
use Symfony \Contracts \HttpClient \HttpClientInterface ;
6
7
use SymfonyDocsBuilder \Phar \Compiler ;
8
+ use SymfonyDocsBuilder \Release \Exception \ReleaseFailed ;
7
9
8
10
class Releaser
9
11
{
@@ -21,7 +23,7 @@ public function __construct(HttpClientInterface $client, Compiler $compiler)
21
23
$ this ->compiler = $ compiler ;
22
24
}
23
25
24
- public function createRelease (string $ tag , string $ name = 'Symfony docs builder %s ' , string $ description = 'Symfony docs builder %s ' )
26
+ public function createRelease (string $ tag , string $ name = 'Symfony docs builder %s ' , string $ description = 'Symfony docs builder %s ' ): void
25
27
{
26
28
if (!preg_match ('/^v\d+\.\d+\.\d+$/ ' , $ tag )) {
27
29
throw new \RuntimeException (sprintf ('"%s" is not a valid tag. ' , $ tag ));
@@ -53,15 +55,8 @@ private function createDraftRelease(string $tag, string $name, string $descripti
53
55
);
54
56
55
57
return (int ) $ response ->toArray ()['id ' ];
56
- } catch (\RuntimeException $ exception ) {
57
- if (401 === $ exception ->getCode ()) {
58
- $ message = 'Error while trying to create release: Invalid token. ' ;
59
- } else {
60
- $ message = 'Error while trying to create release. ' ;
61
- }
62
-
63
- // todo: create new exception which can be exploited in ./bin/create_release
64
- throw new \RuntimeException ($ message , 0 , $ exception );
58
+ } catch (HttpExceptionInterface $ exception ) {
59
+ throw ReleaseFailed::whileCreatingDraft ($ exception );
65
60
}
66
61
}
67
62
@@ -81,9 +76,8 @@ private function addAssetToRelease(int $releaseId): void
81
76
'body ' => file_get_contents (__DIR__ .'/../../docs.phar ' ),
82
77
]
83
78
);
84
- } catch (\RuntimeException $ exception ) {
85
- $ this ->deleteRelease ($ releaseId );
86
- throw new \RuntimeException ('Error while adding asset to release. ' , 0 , $ exception );
79
+ } catch (HttpExceptionInterface $ exception ) {
80
+ $ this ->deleteRelease ($ releaseId , ReleaseFailed::whileAttachingAssetToRelease ($ exception ));
87
81
}
88
82
}
89
83
@@ -99,21 +93,22 @@ private function publishRelease(int $releaseId): void
99
93
],
100
94
]
101
95
);
102
- } catch (\RuntimeException $ exception ) {
103
- $ this ->deleteRelease ($ releaseId );
104
- throw new \RuntimeException ('Error while publishing release. Maybe the tag name already exists? ' , 0 , $ exception );
96
+ } catch (HttpExceptionInterface $ exception ) {
97
+ $ this ->deleteRelease ($ releaseId , ReleaseFailed::whilePublishingRelease ($ exception ));
105
98
}
106
99
}
107
100
108
- private function deleteRelease (int $ releaseId ): void
101
+ private function deleteRelease (int $ releaseId, ReleaseFailed $ previous ): void
109
102
{
110
103
try {
111
104
$ this ->client ->request (
112
105
'DELETE ' ,
113
106
sprintf ('https://api.github.com/repos/%s/%s/releases/%s ' , self ::GITHUB_USER , self ::GITHUB_REPO , $ releaseId )
114
107
);
115
- } catch (\ RuntimeException $ exception ) {
116
- throw new \ RuntimeException ( ' Error while deleting release. ' , 0 , $ exception );
108
+ } catch (HttpExceptionInterface $ exception ) {
109
+ throw new DeleteReleaseFailed ( $ previous , $ exception );
117
110
}
111
+
112
+ throw $ previous ;
118
113
}
119
114
}
0 commit comments