Skip to content

Commit

Permalink
Merge pull request #17 from facade/add-version-support
Browse files Browse the repository at this point in the history
Add version support
  • Loading branch information
freekmurze authored Mar 31, 2021
2 parents ef0f5bc + a0e4f73 commit b523b8b
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 4 deletions.
23 changes: 22 additions & 1 deletion src/Flare.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,33 @@ class Flare
/** @var callable|null */
private $previousErrorHandler;

/** @var callable|null */
private $determineVersionCallable;

public static function register(string $apiKey, string $apiSecret = null, ContextDetectorInterface $contextDetector = null, Container $container = null)
{
$client = new Client($apiKey, $apiSecret);

return new static($client, $contextDetector, $container);
}

public function determineVersionUsing($determineVersionCallable)
{
$this->determineVersionCallable = $determineVersionCallable;
}

/**
* @return null|string
*/
public function version()
{
if (! $this->determineVersionCallable) {
return null;
}

return ($this->determineVersionCallable)();
}

public function __construct(Client $client, ContextDetectorInterface $contextDetector = null, Container $container = null, array $middleware = [])
{
$this->client = $client;
Expand Down Expand Up @@ -215,7 +235,8 @@ public function createReport(Throwable $throwable): Report
$report = Report::createForThrowable(
$throwable,
$this->contextDetector->detectCurrentContext(),
$this->applicationPath
$this->applicationPath,
$this->version()
);

return $this->applyMiddlewareToReport($report);
Expand Down
27 changes: 24 additions & 3 deletions src/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class Report
/** @var string */
private $applicationPath;

/** @var ?string */
private $applicationVersion;

/** @var array */
private $userProvidedContext = [];

Expand All @@ -62,16 +65,21 @@ class Report
/** @var string */
private $groupBy ;

public static function createForThrowable(Throwable $throwable, ContextInterface $context, ?string $applicationPath = null): self
{
public static function createForThrowable(
Throwable $throwable,
ContextInterface $context,
?string $applicationPath = null,
?string $version = null
): self {
return (new static())
->setApplicationPath($applicationPath)
->throwable($throwable)
->useContext($context)
->exceptionClass(self::getClassForThrowable($throwable))
->message($throwable->getMessage())
->stackTrace(Stacktrace::createForThrowable($throwable, $applicationPath))
->exceptionContext($throwable);
->exceptionContext($throwable)
->setApplicationVersion($version);
}

protected static function getClassForThrowable(Throwable $throwable): string
Expand Down Expand Up @@ -193,6 +201,18 @@ public function getApplicationPath(): ?string
return $this->applicationPath;
}

public function setApplicationVersion(?string $applicationVersion)
{
$this->applicationVersion = $applicationVersion;

return $this;
}

public function getApplicationVersion(): ?string
{
return $this->applicationVersion;
}

public function view(?View $view)
{
$this->view = $view;
Expand Down Expand Up @@ -273,6 +293,7 @@ public function toArray()
'message_level' => $this->messageLevel,
'open_frame_index' => $this->openFrameIndex,
'application_path' => $this->applicationPath,
'application_version' => $this->applicationVersion,
];
}
}
32 changes: 32 additions & 0 deletions tests/FlareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,36 @@ public function it_can_add_glows()
],
], $glows);
}

/** @test */
public function a_version_callable_can_be_set()
{
$this->assertNull($this->flare->version());

$this->flare->determineVersionUsing(function () {
return '123';
});

$this->assertEquals('123', $this->flare->version());
}

/** @test */
public function it_will_add_the_version_to_the_report()
{
$this->reportException();

$payload = $this->fakeClient->getLastPayload();

$this->assertNull($payload['application_version']);

$this->flare->determineVersionUsing(function () {
return '123';
});

$this->reportException();

$payload = $this->fakeClient->getLastPayload();

$this->assertEquals('123', $payload['application_version']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ stage: null
message_level: null
open_frame_index: null
application_path: null
application_version: null
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ stage: null
message_level: null
open_frame_index: null
application_path: null
application_version: null
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ stage: null
message_level: null
open_frame_index: 0
application_path: null
application_version: null
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ stage: null
message_level: null
open_frame_index: null
application_path: null
application_version: null

0 comments on commit b523b8b

Please sign in to comment.