Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Illuminate/Foundation/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ protected function prepareException(Throwable $e)
*/
protected function mapException(Throwable $e)
{
if (method_exists($e, 'getInnerException') &&
($inner = $e->getInnerException()) instanceof Throwable) {
return $inner;
}

foreach ($this->exceptionMap as $class => $mapper) {
if (is_a($e, $class)) {
return $mapper($e);
Expand Down
50 changes: 50 additions & 0 deletions src/Illuminate/Routing/Exceptions/StreamedResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Illuminate\Routing\Exceptions;

use Illuminate\Http\Response;
use RuntimeException;
use Throwable;

class StreamedResponseException extends RuntimeException
{
/**
* The actual exception thrown during the stream.
*
* @var \Throwable
*/
public $originalException;

/**
* Create a new exception instance.
*
* @param \Throwable $originalException
* @return void
*/
public function __construct(Throwable $originalException)
{
$this->originalException = $originalException;

parent::__construct($originalException->message);
}

/**
* Render the exception.
*
* @return \Illuminate\Http\Response
*/
public function render()
{
return new Response('');
}

/**
* Get the actual exception thrown during the stream.
*
* @return \Throwable
*/
public function getInnerException()
{
return $this->originalException;
}
}
12 changes: 11 additions & 1 deletion src/Illuminate/Routing/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
use Illuminate\Contracts\View\Factory as ViewFactory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Illuminate\Routing\Exceptions\StreamedResponseException;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Throwable;

class ResponseFactory implements FactoryContract
{
Expand Down Expand Up @@ -138,7 +140,15 @@ public function stream($callback, $status = 200, array $headers = [])
*/
public function streamDownload($callback, $name = null, array $headers = [], $disposition = 'attachment')
{
$response = new StreamedResponse($callback, 200, $headers);
$withWrappedException = function () use ($callback) {
try {
$callback();
} catch (Throwable $e) {
throw new StreamedResponseException($e);
}
};

$response = new StreamedResponse($withWrappedException, 200, $headers);

if (! is_null($name)) {
$response->headers->set('Content-Disposition', $response->headers->makeDisposition(
Expand Down