Skip to content

Commit

Permalink
Merge pull request #109 from piotrooo/feature/exceptions
Browse files Browse the repository at this point in the history
Add suffixes for exceptions (close #104)
  • Loading branch information
jcchavezs authored Apr 19, 2020
2 parents e09a2d8 + eb0619a commit d277122
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
/**
* Thrown when passing an invalid argument for a reference
*/
final class InvalidReferenceArgument extends InvalidArgumentException
final class InvalidReferenceArgumentException extends InvalidArgumentException
{
/**
* @return InvalidReferenceArgument
* @return InvalidReferenceArgumentException
*/
public static function forEmptyType(): InvalidReferenceArgument
public static function forEmptyType(): InvalidReferenceArgumentException
{
return new self('Reference type can not be an empty string');
}

/**
* @param mixed $context
* @return InvalidReferenceArgument
* @return InvalidReferenceArgumentException
*/
public static function forInvalidContext($context): InvalidReferenceArgument
public static function forInvalidContext($context): InvalidReferenceArgumentException
{
return new self(sprintf(
'Reference expects \OpenTracing\Span or \OpenTracing\SpanContext as context, got %s',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
/**
* Thrown when a reference has more than one parent in the SpanOptions
*/
final class InvalidReferencesSet extends DomainException
final class InvalidReferencesSetException extends DomainException
{
/**
* @param string $message
* @return InvalidReferencesSet
* @return InvalidReferencesSetException
*/
public static function create(string $message): InvalidReferencesSet
public static function create(string $message): InvalidReferencesSetException
{
return new self($message);
}

/**
* @return InvalidReferencesSet
* @return InvalidReferencesSetException
*/
public static function forMoreThanOneParent(): InvalidReferencesSet
public static function forMoreThanOneParent(): InvalidReferencesSetException
{
return new self('Span can not have more than one parent, either one as child_of or either one as follows_from');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@
/**
* Thrown when passing an invalid option on Span creation
*/
final class InvalidSpanOption extends InvalidArgumentException
final class InvalidSpanOptionException extends InvalidArgumentException
{
/**
* @return InvalidSpanOption
* @return InvalidSpanOptionException
*/
public static function forIncludingBothChildOfAndReferences(): InvalidSpanOption
public static function forIncludingBothChildOfAndReferences(): InvalidSpanOptionException
{
return new self('Either "childOf" or "references" options are accepted but not both.');
}

/**
* @param mixed $reference
* @return InvalidSpanOption
* @return InvalidSpanOptionException
*/
public static function forInvalidReference($reference): InvalidSpanOption
public static function forInvalidReference($reference): InvalidSpanOptionException
{
return new self(sprintf(
'Invalid reference. Expected OpenTracing\Reference, got %s.',
Expand All @@ -32,18 +32,18 @@ public static function forInvalidReference($reference): InvalidSpanOption
}

/**
* @return InvalidSpanOption
* @return InvalidSpanOptionException
*/
public static function forInvalidStartTime(): InvalidSpanOption
public static function forInvalidStartTime(): InvalidSpanOptionException
{
return new self('Invalid start_time option. Expected int or float got string.');
}

/**
* @param mixed $childOfOption
* @return InvalidSpanOption
* @return InvalidSpanOptionException
*/
public static function forInvalidChildOf($childOfOption): InvalidSpanOption
public static function forInvalidChildOf($childOfOption): InvalidSpanOptionException
{
return new self(sprintf(
'Invalid child_of option. Expected Span or SpanContext, got %s',
Expand All @@ -53,27 +53,27 @@ public static function forInvalidChildOf($childOfOption): InvalidSpanOption

/**
* @param string $key
* @return InvalidSpanOption
* @return InvalidSpanOptionException
*/
public static function forUnknownOption(string $key): InvalidSpanOption
public static function forUnknownOption(string $key): InvalidSpanOptionException
{
return new self(sprintf('Invalid option %s.', $key));
}

/**
* @param mixed $tag
* @return InvalidSpanOption
* @return InvalidSpanOptionException
*/
public static function forInvalidTag($tag): InvalidSpanOption
public static function forInvalidTag($tag): InvalidSpanOptionException
{
return new self(sprintf('Invalid tag. Expected string, got %s', gettype($tag)));
}

/**
* @param mixed $tagValue
* @return InvalidSpanOption
* @return InvalidSpanOptionException
*/
public static function forInvalidTagValue($tagValue): InvalidSpanOption
public static function forInvalidTagValue($tagValue): InvalidSpanOptionException
{
return new self(sprintf(
'Invalid tag value. Expected scalar or object with __toString method, got %s',
Expand All @@ -83,9 +83,9 @@ public static function forInvalidTagValue($tagValue): InvalidSpanOption

/**
* @param mixed $value
* @return InvalidSpanOption
* @return InvalidSpanOptionException
*/
public static function forInvalidTags($value): InvalidSpanOption
public static function forInvalidTags($value): InvalidSpanOptionException
{
return new self(sprintf(
'Invalid tags value. Expected a associative array of tags, got %s',
Expand All @@ -95,9 +95,9 @@ public static function forInvalidTags($value): InvalidSpanOption

/**
* @param mixed $value
* @return InvalidSpanOption
* @return InvalidSpanOptionException
*/
public static function forInvalidReferenceSet($value): InvalidSpanOption
public static function forInvalidReferenceSet($value): InvalidSpanOptionException
{
return new self(sprintf(
'Invalid references set. Expected Reference or Reference[], got %s',
Expand All @@ -107,9 +107,9 @@ public static function forInvalidReferenceSet($value): InvalidSpanOption

/**
* @param mixed $value
* @return InvalidSpanOption
* @return InvalidSpanOptionException
*/
public static function forFinishSpanOnClose($value): InvalidSpanOption
public static function forFinishSpanOnClose($value): InvalidSpanOptionException
{
return new self(sprintf(
'Invalid type for finish_span_on_close. Expected bool, got %s',
Expand All @@ -119,9 +119,9 @@ public static function forFinishSpanOnClose($value): InvalidSpanOption

/**
* @param mixed $value
* @return InvalidSpanOption
* @return InvalidSpanOptionException
*/
public static function forIgnoreActiveSpan($value): InvalidSpanOption
public static function forIgnoreActiveSpan($value): InvalidSpanOptionException
{
return new self(sprintf(
'Invalid type for ignore_active_span. Expected bool, got %s',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
/**
* Thrown when trying to inject or extract in an invalid format
*/
final class UnsupportedFormat extends UnexpectedValueException
final class UnsupportedFormatException extends UnexpectedValueException
{
/**
* @param string $format
* @return UnsupportedFormat
* @return UnsupportedFormatException
*/
public static function forFormat(string $format): UnsupportedFormat
public static function forFormat(string $format): UnsupportedFormatException
{
return new self(sprintf('The format \'%s\' is not supported.', $format));
}
Expand Down
10 changes: 5 additions & 5 deletions src/OpenTracing/Mock/MockTracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace OpenTracing\Mock;

use OpenTracing\Exceptions\InvalidReferenceArgument;
use OpenTracing\Exceptions\UnsupportedFormat;
use OpenTracing\Exceptions\InvalidReferenceArgumentException;
use OpenTracing\Exceptions\UnsupportedFormatException;
use OpenTracing\Scope;
use OpenTracing\ScopeManager;
use OpenTracing\Span;
Expand Down Expand Up @@ -74,7 +74,7 @@ public function startSpan(string $operationName, $options = []): Span
} else {
$referenceContext = $options->getReferences()[0]->getSpanContext();
if (!$referenceContext instanceof MockSpanContext) {
throw InvalidReferenceArgument::forInvalidContext($referenceContext);
throw InvalidReferenceArgumentException::forInvalidContext($referenceContext);
}
$spanContext = MockSpanContext::createAsChildOf($referenceContext);
}
Expand All @@ -96,7 +96,7 @@ public function startSpan(string $operationName, $options = []): Span
public function inject(SpanContext $spanContext, string $format, &$carrier): void
{
if (!array_key_exists($format, $this->injectors)) {
throw UnsupportedFormat::forFormat($format);
throw UnsupportedFormatException::forFormat($format);
}

$this->injectors[$format]($spanContext, $carrier);
Expand All @@ -108,7 +108,7 @@ public function inject(SpanContext $spanContext, string $format, &$carrier): voi
public function extract(string $format, $carrier): ?SpanContext
{
if (!array_key_exists($format, $this->extractors)) {
throw UnsupportedFormat::forFormat($format);
throw UnsupportedFormatException::forFormat($format);
}

return $this->extractors[$format]($carrier);
Expand Down
6 changes: 3 additions & 3 deletions src/OpenTracing/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace OpenTracing;

use OpenTracing\Exceptions\InvalidReferenceArgument;
use OpenTracing\Exceptions\InvalidReferenceArgumentException;

final class Reference
{
Expand Down Expand Up @@ -38,7 +38,7 @@ final class Reference
public function __construct(string $type, SpanContext $spanContext)
{
if (empty($type)) {
throw InvalidReferenceArgument::forEmptyType();
throw InvalidReferenceArgumentException::forEmptyType();
}

$this->type = $type;
Expand All @@ -49,7 +49,7 @@ public function __construct(string $type, SpanContext $spanContext)
* @param string $type
* @param Span $span
* @return Reference when context is invalid
* @throws InvalidReferenceArgument on empty type
* @throws InvalidReferenceArgumentException on empty type
*/
public static function createForSpan(string $type, Span $span): Reference
{
Expand Down
30 changes: 15 additions & 15 deletions src/OpenTracing/StartSpanOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use DateTime;
use DateTimeInterface;
use OpenTracing\Exceptions\InvalidReferencesSet;
use OpenTracing\Exceptions\InvalidSpanOption;
use OpenTracing\Exceptions\InvalidReferencesSetException;
use OpenTracing\Exceptions\InvalidSpanOptionException;

final class StartSpanOptions
{
Expand Down Expand Up @@ -41,8 +41,8 @@ final class StartSpanOptions
/**
* @param array $options
* @return StartSpanOptions
* @throws InvalidReferencesSet when there are inconsistencies about the references
* @throws InvalidSpanOption when one of the options is invalid
* @throws InvalidReferencesSetException when there are inconsistencies about the references
* @throws InvalidSpanOptionException when one of the options is invalid
*/
public static function create(array $options): StartSpanOptions
{
Expand All @@ -52,35 +52,35 @@ public static function create(array $options): StartSpanOptions
switch ($key) {
case 'child_of':
if (!empty($spanOptions->references)) {
throw InvalidSpanOption::forIncludingBothChildOfAndReferences();
throw InvalidSpanOptionException::forIncludingBothChildOfAndReferences();
}

$spanOptions->references[] = self::buildChildOf($value);
break;

case 'references':
if (!empty($spanOptions->references)) {
throw InvalidSpanOption::forIncludingBothChildOfAndReferences();
throw InvalidSpanOptionException::forIncludingBothChildOfAndReferences();
}

if ($value instanceof Reference) {
$spanOptions->references = [$value];
} elseif (is_array($value)) {
$spanOptions->references = self::buildReferences($value);
} else {
throw InvalidSpanOption::forInvalidReferenceSet($value);
throw InvalidSpanOptionException::forInvalidReferenceSet($value);
}

break;

case 'tags':
if (!is_array($value)) {
throw InvalidSpanOption::forInvalidTags($value);
throw InvalidSpanOptionException::forInvalidTags($value);
}

foreach ($value as $tag => $tagValue) {
if ($tag !== (string)$tag) {
throw InvalidSpanOption::forInvalidTag($tag);
throw InvalidSpanOptionException::forInvalidTag($tag);
}

$spanOptions->tags[$tag] = $tagValue;
Expand All @@ -89,30 +89,30 @@ public static function create(array $options): StartSpanOptions

case 'start_time':
if (is_scalar($value) && !is_numeric($value)) {
throw InvalidSpanOption::forInvalidStartTime();
throw InvalidSpanOptionException::forInvalidStartTime();
}

$spanOptions->startTime = $value;
break;

case 'finish_span_on_close':
if (!is_bool($value)) {
throw InvalidSpanOption::forFinishSpanOnClose($value);
throw InvalidSpanOptionException::forFinishSpanOnClose($value);
}

$spanOptions->finishSpanOnClose = $value;
break;

case 'ignore_active_span':
if (!is_bool($value)) {
throw InvalidSpanOption::forIgnoreActiveSpan($value);
throw InvalidSpanOptionException::forIgnoreActiveSpan($value);
}

$spanOptions->ignoreActiveSpan = $value;
break;

default:
throw InvalidSpanOption::forUnknownOption($key);
throw InvalidSpanOptionException::forUnknownOption($key);
}
}

Expand Down Expand Up @@ -186,7 +186,7 @@ private static function buildChildOf($value): Reference
return new Reference(Reference::CHILD_OF, $value);
}

throw InvalidSpanOption::forInvalidChildOf($value);
throw InvalidSpanOptionException::forInvalidChildOf($value);
}

private static function buildReferences(array $referencesArray): array
Expand All @@ -195,7 +195,7 @@ private static function buildReferences(array $referencesArray): array

foreach ($referencesArray as $reference) {
if (!($reference instanceof Reference)) {
throw InvalidSpanOption::forInvalidReference($reference);
throw InvalidSpanOptionException::forInvalidReference($reference);
}

$references[] = $reference;
Expand Down
Loading

0 comments on commit d277122

Please sign in to comment.