Skip to content

Commit 2b8894d

Browse files
Catch Connection Exception (#4)
Fixed: Catch connection exceptions in addition to errors from the API itself
1 parent 8a47c0e commit 2b8894d

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

config/torchlight.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
return [
44
// The Torchlight client caches highlighted code blocks. Here
5-
// you can define which cache driver you'd like to use.
5+
// you can define which cache driver you'd like to use. If
6+
// leave this blank your default app cache will be used.
67
'cache' => env('TORCHLIGHT_CACHE_DRIVER'),
78

89
// Which theme you want to use. You can find all of the themes at
9-
// https://torchlight.dev/themes, or you can provide your own.
10+
// https://torchlight.dev/themes.
1011
'theme' => env('TORCHLIGHT_THEME', 'material-theme-palenight'),
1112

1213
// Your API token from torchlight.dev.

src/Client.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Support\Collection;
1010
use Illuminate\Support\Facades\Cache;
1111
use Illuminate\Support\Facades\Http;
12+
use Throwable;
1213
use Torchlight\Exceptions\ConfigurationException;
1314
use Torchlight\Exceptions\RequestException;
1415
use Torchlight\Exceptions\TorchlightException;
@@ -45,12 +46,18 @@ protected function request(Collection $blocks)
4546

4647
$host = Torchlight::config('host', 'https://api.torchlight.dev');
4748

48-
$response = Http::timeout(5)
49-
->withToken($this->getToken())
50-
->post($host . '/highlight', [
51-
'blocks' => $this->blocksAsRequestParam($blocks)->values()->toArray(),
52-
])
53-
->json();
49+
try {
50+
$response = Http::timeout(5)
51+
->withToken($this->getToken())
52+
->post($host . '/highlight', [
53+
'blocks' => $this->blocksAsRequestParam($blocks)->values()->toArray(),
54+
])
55+
->json();
56+
} catch (Throwable $e) {
57+
$response = [
58+
'error' => $e->getMessage()
59+
];
60+
}
5461

5562
$this->potentiallyThrowRequestException($response);
5663

tests/ClientTimeoutTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <[email protected]|https://twitter.com/aarondfrancis>
4+
*/
5+
6+
namespace Torchlight\Tests;
7+
8+
use Torchlight\Block;
9+
use Torchlight\Exceptions\RequestException;
10+
use Torchlight\Torchlight;
11+
12+
class ClientTimeoutTest extends BaseTest
13+
{
14+
public function getEnvironmentSetUp($app)
15+
{
16+
config()->set('torchlight', [
17+
'theme' => 'material',
18+
'token' => 'token',
19+
'bust' => 0,
20+
'host' => 'https://nonexistent.torchlight.dev'
21+
]);
22+
}
23+
24+
/** @test */
25+
public function it_catches_the_connect_exception()
26+
{
27+
// Our exception, not the default Laravel one.
28+
$this->expectException(RequestException::class);
29+
30+
Torchlight::highlight(
31+
Block::make('id')->language('php')->code('echo "hello world";')
32+
);
33+
}
34+
35+
/** @test */
36+
public function it_catches_the_connect_exception_in_prod()
37+
{
38+
Torchlight::overrideEnvironment('production');
39+
40+
Torchlight::highlight(
41+
Block::make('id')->language('php')->code('echo "hello world";')
42+
);
43+
44+
// Just want to make sure we got past the highlight with no exception.
45+
$this->assertTrue(true);
46+
}
47+
}

0 commit comments

Comments
 (0)