Skip to content

Commit d102edc

Browse files
authored
Merge pull request #96 from getsentry/improve-examples-for-prod-usage
Improve examples for prod usage
2 parents b97bbc0 + 9262b9a commit d102edc

File tree

13 files changed

+286
-41
lines changed

13 files changed

+286
-41
lines changed

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,30 @@ class SentryContext
199199
}
200200
```
201201

202+
## Displaying the error ID
203+
204+
When something goes wrong and you get a customer email in your inbox it would be nice if they could give you some kind of identitifier for the error they are seeing.
205+
206+
Luckily Sentry provides you with just that by adding one of the following options to your error view.
207+
208+
```php
209+
// Using the Sentry facade
210+
$errorID = Sentry::getLastEventID();
211+
212+
// Or without the Sentry facade (Lumen)
213+
$errorID = app('sentry')->getLastEventID();
214+
```
215+
216+
This could look something like this in for example your `resources/views/error/500.blade.php`:
217+
218+
```blade
219+
@if(!empty(Sentry::getLastEventID()))
220+
<p>Please send this ID with your support request: {{ Sentry::getLastEventID() }}.</p>
221+
@endif
222+
```
223+
224+
This ID can be searched for in the Sentry interface allowing you to find the error quickly.
225+
202226

203227
## Contributing
204228

examples/laravel-4.2/app/config/app.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@
183183
'Route' => 'Illuminate\Support\Facades\Route',
184184
'Schema' => 'Illuminate\Support\Facades\Schema',
185185
'Seeder' => 'Illuminate\Database\Seeder',
186-
'Sentry' => 'Sentry\SentryLaravel\SentryFacade',
186+
'Sentry' => 'Sentry\SentryLaravel\SentryFacade',
187187
'Session' => 'Illuminate\Support\Facades\Session',
188188
'SoftDeletingTrait' => 'Illuminate\Database\Eloquent\SoftDeletingTrait',
189189
'SSH' => 'Illuminate\Support\Facades\SSH',

examples/laravel-4.2/app/start/global.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@
4949
App::error(function(Exception $exception, $code)
5050
{
5151
Log::error($exception);
52+
53+
// If we have a custom error page for the code return that
54+
// unless we are debugging since then we want to show the
55+
// debug screen with the stacktrace and error info etc.
56+
if (!Config::get('app.debug') && View::exists("errors.{$code}")) {
57+
return Response::view("errors.{$code}", array(), $code);
58+
}
5259
});
5360

5461
/*
@@ -64,7 +71,7 @@
6471

6572
App::down(function()
6673
{
67-
return Response::make("Be right back!", 503);
74+
return Response::view('errors.503', array(), 503);
6875
});
6976

7077
/*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Internal Server Error</title>
5+
6+
<link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">
7+
8+
<style>
9+
html, body {
10+
height: 100%;
11+
}
12+
13+
body {
14+
margin: 0;
15+
padding: 0;
16+
width: 100%;
17+
color: #B0BEC5;
18+
display: table;
19+
font-weight: 100;
20+
font-family: 'Lato';
21+
}
22+
23+
.container {
24+
text-align: center;
25+
display: table-cell;
26+
vertical-align: middle;
27+
}
28+
29+
.content {
30+
text-align: center;
31+
display: inline-block;
32+
}
33+
34+
.title {
35+
font-size: 72px;
36+
margin-bottom: 40px;
37+
}
38+
</style>
39+
</head>
40+
<body>
41+
<div class="container">
42+
<div class="content">
43+
<div class="title">Something went wrong.</div>
44+
@if(!empty(Sentry::getLastEventID()))
45+
<div class="subtitle">Error ID: {{ Sentry::getLastEventID() }}</div>
46+
@endif
47+
@unless(empty($sentryID))
48+
<!-- Sentry JS SDK 2.1.+ required -->
49+
<script src="https://cdn.ravenjs.com/3.3.0/raven.min.js"></script>
50+
51+
<script>
52+
Raven.showReportDialog({
53+
eventId: '{{ Sentry::getLastEventID() }}',
54+
55+
// use the public DSN (dont include your secret!)
56+
dsn: 'https://[email protected]/3235',
57+
58+
user: {
59+
'name': 'Jane Doe',
60+
'email': '[email protected]',
61+
}
62+
});
63+
</script>
64+
@endunless
65+
</div>
66+
</div>
67+
</body>
68+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<html>
2+
<head>
3+
<title>Be right back.</title>
4+
5+
<link href='//fonts.googleapis.com/css?family=Lato:100' rel='stylesheet' type='text/css'>
6+
7+
<style>
8+
body {
9+
margin: 0;
10+
padding: 0;
11+
width: 100%;
12+
height: 100%;
13+
color: #B0BEC5;
14+
display: table;
15+
font-weight: 100;
16+
font-family: 'Lato';
17+
}
18+
19+
.container {
20+
text-align: center;
21+
display: table-cell;
22+
vertical-align: middle;
23+
}
24+
25+
.content {
26+
text-align: center;
27+
display: inline-block;
28+
}
29+
30+
.title {
31+
font-size: 72px;
32+
margin-bottom: 40px;
33+
}
34+
</style>
35+
</head>
36+
<body>
37+
<div class="container">
38+
<div class="content">
39+
<div class="title">Be right back.</div>
40+
</div>
41+
</div>
42+
</body>
43+
</html>

examples/laravel-4.2/bootstrap/start.php

+3
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,7 @@
7070
|
7171
*/
7272

73+
// Ignore deprecated messages on PHP 7+ for the OpenSSL extension.
74+
error_reporting(E_ALL ^ E_DEPRECATED);
75+
7376
return $app;

examples/laravel-5.1/app/Exceptions/Handler.php

+19-8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
7+
use Symfony\Component\HttpKernel\Exception\HttpException;
78

89
class Handler extends ExceptionHandler
910
{
@@ -21,26 +22,36 @@ class Handler extends ExceptionHandler
2122
*
2223
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
2324
*
24-
* @param \Exception $e
25+
* @param \Exception $e
26+
*
2527
* @return void
2628
*/
2729
public function report(Exception $e)
2830
{
29-
if ($this->shouldReport($e)) {
31+
if (app()->bound('sentry') && $this->shouldReport($e)) {
3032
app('sentry')->captureException($e);
3133
}
34+
3235
return parent::report($e);
3336
}
3437

3538
/**
36-
* Render an exception into an HTTP response.
39+
* Render an exception into a response.
40+
*
41+
* @param \Illuminate\Http\Request $request
42+
* @param \Exception $exception
3743
*
38-
* @param \Illuminate\Http\Request $request
39-
* @param \Exception $e
40-
* @return \Illuminate\Http\Response
44+
* @return \Symfony\Component\HttpFoundation\Response
4145
*/
42-
public function render($request, Exception $e)
46+
public function render($request, Exception $exception)
4347
{
44-
return parent::render($request, $e);
48+
// Convert all non-http exceptions to a proper 500 http exception
49+
// if we don't do this exceptions are shown as a default template
50+
// instead of our own view in resources/views/errors/500.blade.php
51+
if (!$this->isHttpException($exception) && !config('app.debug')) {
52+
$exception = new HttpException(500, 'Whoops!');
53+
}
54+
55+
return parent::render($request, $exception);
4556
}
4657
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Internal Server Error</title>
5+
6+
<link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">
7+
8+
<style>
9+
html, body {
10+
height: 100%;
11+
}
12+
13+
body {
14+
margin: 0;
15+
padding: 0;
16+
width: 100%;
17+
color: #B0BEC5;
18+
display: table;
19+
font-weight: 100;
20+
font-family: 'Lato';
21+
}
22+
23+
.container {
24+
text-align: center;
25+
display: table-cell;
26+
vertical-align: middle;
27+
}
28+
29+
.content {
30+
text-align: center;
31+
display: inline-block;
32+
}
33+
34+
.title {
35+
font-size: 72px;
36+
margin-bottom: 40px;
37+
}
38+
</style>
39+
</head>
40+
<body>
41+
<div class="container">
42+
<div class="content">
43+
<div class="title">Something went wrong.</div>
44+
@if(!empty(Sentry::getLastEventID()))
45+
<div class="subtitle">Error ID: {{ Sentry::getLastEventID() }}</div>
46+
@endif
47+
@unless(empty($sentryID))
48+
<!-- Sentry JS SDK 2.1.+ required -->
49+
<script src="https://cdn.ravenjs.com/3.3.0/raven.min.js"></script>
50+
51+
<script>
52+
Raven.showReportDialog({
53+
eventId: '{{ Sentry::getLastEventID() }}',
54+
55+
// use the public DSN (dont include your secret!)
56+
dsn: 'https://[email protected]/3235',
57+
58+
user: {
59+
'name': 'Jane Doe',
60+
'email': '[email protected]',
61+
}
62+
});
63+
</script>
64+
@endunless
65+
</div>
66+
</div>
67+
</body>
68+
</html>

examples/laravel-5.2/app/Exceptions/Handler.php

+19-13
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,41 @@ class Handler extends ExceptionHandler
2323
ValidationException::class,
2424
];
2525

26-
private $sentryID;
27-
2826
/**
2927
* Report or log an exception.
3028
*
3129
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
3230
*
33-
* @param \Exception $e
31+
* @param \Exception $e
32+
*
3433
* @return void
3534
*/
3635
public function report(Exception $e)
3736
{
38-
if ($this->shouldReport($e)) {
39-
$this->sentryID = app('sentry')->captureException($e);
37+
if (app()->bound('sentry') && $this->shouldReport($e)) {
38+
app('sentry')->captureException($e);
4039
}
40+
4141
parent::report($e);
4242
}
4343

4444
/**
45-
* Render an exception into an HTTP response.
45+
* Render an exception into a response.
4646
*
47-
* @param \Illuminate\Http\Request $request
48-
* @param \Exception $e
49-
* @return \Illuminate\Http\Response
47+
* @param \Illuminate\Http\Request $request
48+
* @param \Exception $exception
49+
*
50+
* @return \Symfony\Component\HttpFoundation\Response
5051
*/
51-
public function render($request, Exception $e)
52+
public function render($request, Exception $exception)
5253
{
53-
return response()->view('errors.500', [
54-
'sentryID' => $this->sentryID,
55-
], 500);
54+
// Convert all non-http exceptions to a proper 500 http exception
55+
// if we don't do this exceptions are shown as a default template
56+
// instead of our own view in resources/views/errors/500.blade.php
57+
if (!$this->isHttpException($exception) && !config('app.debug')) {
58+
$exception = new HttpException(500, 'Whoops!');
59+
}
60+
61+
return parent::render($request, $exception);
5662
}
5763
}

examples/laravel-5.2/resources/views/errors/500.blade.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,16 @@
4141
<div class="container">
4242
<div class="content">
4343
<div class="title">Something went wrong.</div>
44+
@if(!empty(Sentry::getLastEventID()))
45+
<div class="subtitle">Error ID: {{ Sentry::getLastEventID() }}</div>
46+
@endif
4447
@unless(empty($sentryID))
4548
<!-- Sentry JS SDK 2.1.+ required -->
4649
<script src="https://cdn.ravenjs.com/3.3.0/raven.min.js"></script>
4750

4851
<script>
4952
Raven.showReportDialog({
50-
eventId: '{{ $sentryID }}',
53+
eventId: '{{ Sentry::getLastEventID() }}',
5154
5255
// use the public DSN (dont include your secret!)
5356
dsn: 'https://[email protected]/3235',

0 commit comments

Comments
 (0)