Skip to content

Commit 803b517

Browse files
committed
Cleanup, throw exceptions
1 parent 2f2a6ba commit 803b517

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

config/torchlight.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
return [
44
// The Torchlight client caches highlighted code blocks. Here
55
// you can define which cache driver you'd like to use.
6-
'cache' => env('TORCHLIGHT_CACHE_DRIVER', 'default'),
6+
'cache' => env('TORCHLIGHT_CACHE_DRIVER'),
77

88
// Which theme you want to use. You can find all of the themes at
99
// https://torchlight.dev/themes, or you can provide your own.
@@ -14,4 +14,4 @@
1414

1515
// If you want to register the blade directives, set this to true.
1616
'blade_directives' => true,
17-
];
17+
];

src/Client.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace Hammerstone\Torchlight;
77

8+
use Hammerstone\Torchlight\Exceptions\ConfigurationException;
9+
use Hammerstone\Torchlight\Exceptions\RequestException;
810
use Illuminate\Http\Client\Response;
911
use Illuminate\Support\Arr;
1012
use Illuminate\Support\Collection;
@@ -40,12 +42,14 @@ protected function request(Collection $blocks)
4042
$host = config('torchlight.host', 'https://api.torchlight.dev');
4143

4244
$response = Http::timeout(5)
43-
->withToken(config('torchlight.token'))
45+
->withToken($this->getToken())
4446
->post($host . '/highlight', [
4547
'blocks' => $this->blocksAsRequestParam($blocks)->values(),
4648
])
4749
->json();
4850

51+
$this->potentiallyThrowRequestException($response);
52+
4953
$response = collect(Arr::get($response, 'blocks', []))->keyBy('id');
5054

5155
$blocks->each(function (Block $block) use ($response) {
@@ -60,6 +64,31 @@ protected function request(Collection $blocks)
6064
return $blocks;
6165
}
6266

67+
protected function getToken()
68+
{
69+
$token = config('torchlight.token');
70+
71+
if (!$token) {
72+
$this->throwUnlessProduction(
73+
new ConfigurationException('No Torchlight token configured.')
74+
);
75+
}
76+
77+
return $token;
78+
}
79+
80+
protected function potentiallyThrowRequestException($response)
81+
{
82+
if ($error = Arr::get($response, 'error')) {
83+
$this->throwUnlessProduction(new RequestException($error));
84+
}
85+
}
86+
87+
protected function throwUnlessProduction($exception)
88+
{
89+
throw_unless(app()->environment('production'), $exception);
90+
}
91+
6392
public function cache()
6493
{
6594
$store = config('torchlight.cache');
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <aarondfrancis@gmail.com|https://twitter.com/aarondfrancis>
4+
*/
5+
6+
namespace Hammerstone\Torchlight\Exceptions;
7+
8+
9+
class ConfigurationException extends TorchlightException
10+
{
11+
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <aarondfrancis@gmail.com|https://twitter.com/aarondfrancis>
4+
*/
5+
6+
namespace Hammerstone\Torchlight\Exceptions;
7+
8+
9+
class RequestException extends TorchlightException
10+
{
11+
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <aaron@hammerstone.dev|https://twitter.com/aarondfrancis>
4+
*/
5+
6+
namespace Hammerstone\Torchlight\Exceptions;
7+
8+
class TorchlightException extends \Exception
9+
{
10+
11+
}

0 commit comments

Comments
 (0)