| 1 | package GraphQL::Error; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 21 | ▶ | use 5.014; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 21 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 21 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 21 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 21 | ▶ | use Types::Standard -all; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 21 | ▶ | use GraphQL::Type::Library -all; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 21 | ▶ | use GraphQL::MaybeTypeCheck; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 21 | ▶ | use GraphQL::Debug qw(_debug); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | 21 | our $VERSION = '0.02'; | |||||||||||||
| 13 | |||||||||||||||
| 14 | 21 | ▶ | use constant DEBUG => $ENV{GRAPHQL_DEBUG}; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 21 | my %NONENUM = map { ($_ => 1) } qw(original_error); | |||||||||||||
| 16 | 21 | ▶ | use overload '""' => 'to_string'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | |||||||||||||||
| 18 | =head1 NAME | ||||||||||||||
| 19 | |||||||||||||||
| 20 | GraphQL::Error - GraphQL error object | ||||||||||||||
| 21 | |||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||
| 23 | |||||||||||||||
| 24 | use GraphQL::Error; | ||||||||||||||
| 25 | die GraphQL::Error->new(message => 'Something is not right...'); | ||||||||||||||
| 26 | |||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||
| 28 | |||||||||||||||
| 29 | Class implementing GraphQL error object. | ||||||||||||||
| 30 | |||||||||||||||
| 31 | =head1 ATTRIBUTES | ||||||||||||||
| 32 | |||||||||||||||
| 33 | =head2 message | ||||||||||||||
| 34 | |||||||||||||||
| 35 | =cut | ||||||||||||||
| 36 | |||||||||||||||
| 37 | 21 | has message => (is => 'ro', isa => Str, required => 1); | |||||||||||||
| 38 | |||||||||||||||
| 39 | =head2 original_error | ||||||||||||||
| 40 | |||||||||||||||
| 41 | If there is an original error to be preserved. | ||||||||||||||
| 42 | |||||||||||||||
| 43 | =cut | ||||||||||||||
| 44 | |||||||||||||||
| 45 | 21 | has original_error => (is => 'ro', isa => Any); | |||||||||||||
| 46 | |||||||||||||||
| 47 | =head2 locations | ||||||||||||||
| 48 | |||||||||||||||
| 49 | Array-ref of L<GraphQL::Type::Library/DocumentLocation>s. | ||||||||||||||
| 50 | |||||||||||||||
| 51 | =cut | ||||||||||||||
| 52 | |||||||||||||||
| 53 | 21 | has locations => (is => 'ro', isa => ArrayRef[DocumentLocation]); | |||||||||||||
| 54 | |||||||||||||||
| 55 | =head2 path | ||||||||||||||
| 56 | |||||||||||||||
| 57 | Array-ref of L<GraphQL::Type::Library/StrNameValid>s or C<Int>s describing | ||||||||||||||
| 58 | the path from the top operation (being either fields, or a List offset). | ||||||||||||||
| 59 | |||||||||||||||
| 60 | =cut | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 21 | has path => (is => 'ro', isa => ArrayRef[StrNameValid | Int]); | |||||||||||||
| 63 | |||||||||||||||
| 64 | =head2 extensions | ||||||||||||||
| 65 | |||||||||||||||
| 66 | Hash-ref of L<GraphQL::Type::Library/JSONable>s providing additional | ||||||||||||||
| 67 | information. | ||||||||||||||
| 68 | |||||||||||||||
| 69 | =cut | ||||||||||||||
| 70 | |||||||||||||||
| 71 | 21 | has extensions => (is => 'ro', isa => Optional[HashRef[JSONable]]); | |||||||||||||
| 72 | |||||||||||||||
| 73 | =head1 METHODS | ||||||||||||||
| 74 | |||||||||||||||
| 75 | =head2 is | ||||||||||||||
| 76 | |||||||||||||||
| 77 | Is the supplied scalar an error object? | ||||||||||||||
| 78 | |||||||||||||||
| 79 | =cut | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 21 | ▶ | method is(Any $item) :ReturnType(Bool) { ref $item eq __PACKAGE__ } | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
is: called CC=3 SCAR=11.1
Pod
documented
Branch: 2 branches
| |||||||||||||||
| 82 | |||||||||||||||
| 83 | =head2 coerce | ||||||||||||||
| 84 | |||||||||||||||
| 85 | If supplied scalar is an error object, return. If not, return one with | ||||||||||||||
| 86 | it as message. If an object, message will be stringified version of that, | ||||||||||||||
| 87 | it will be preserved as C<original_error>. | ||||||||||||||
| 88 | |||||||||||||||
| 89 | =cut | ||||||||||||||
| 90 | |||||||||||||||
| 91 | 367 | ▶ | method coerce( | ||||||||||||
|
Subroutine
coerce: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||
| 92 | Any $item | ||||||||||||||
| 93 | 367 | ▶ | ) :ReturnType(InstanceOf[__PACKAGE__]) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 94 | 367 | DEBUG and _debug('Error.coerce', $item); | |||||||||||||
| 95 | 367 | ▶ | return $item if __PACKAGE__->is($item); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 96 | 85 | ▶ | $item ||= 'Unknown error'; | ||||||||||||
|
Condition50%1/2
$item ||= "Unknown error"
MC/DC0%0/1
$item ||= "Unknown error"
$item
Truth table50%1/2
A
A= $item
| |||||||||||||||
| 97 | 85 | ▶ | !is_Str($item) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | ? $self->new(message => $item.'', original_error => $item) | ||||||||||||||
| 99 | : $self->new(message => $item); | ||||||||||||||
| 100 | 21 | ▶ | } | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 101 | |||||||||||||||
| 102 | =head2 but | ||||||||||||||
| 103 | |||||||||||||||
| 104 | Returns a copy of the error object, but with the given properties (as | ||||||||||||||
| 105 | with a C<new> method, not coincidentally) overriding the existing ones. | ||||||||||||||
| 106 | |||||||||||||||
| 107 | =cut | ||||||||||||||
| 108 | |||||||||||||||
| 109 | 76 | ▶ | sub but :ReturnType(InstanceOf[__PACKAGE__]) { | ||||||||||||
|
Subroutine
but: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 110 | 76 | my $self = shift; | |||||||||||||
| 111 | 76 | $self->new(%$self, @_); | |||||||||||||
| 112 | 21 | ▶ | } | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 113 | |||||||||||||||
| 114 | =head2 to_string | ||||||||||||||
| 115 | |||||||||||||||
| 116 | Converts to string. | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =cut | ||||||||||||||
| 119 | |||||||||||||||
| 120 | 126 | ▶ | method to_string(@ignore) :ReturnType(Str) { | ||||||||||||
|
Subroutine
to_string: called CC=2 SCAR=7.0
Pod
documented
Branch: 1 branch
| |||||||||||||||
| 121 | 126 | $self->message; | |||||||||||||
| 122 | 21 | ▶ | } | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 123 | |||||||||||||||
| 124 | =head2 to_json | ||||||||||||||
| 125 | |||||||||||||||
| 126 | Converts to a JSON-able hash, in the format to send back as a member of | ||||||||||||||
| 127 | the C<errors> array in the results. | ||||||||||||||
| 128 | |||||||||||||||
| 129 | =cut | ||||||||||||||
| 130 | |||||||||||||||
| 131 | 99 | ▶ | method to_json() :ReturnType(HashRef) { | ||||||||||||
|
Subroutine
to_json: called CC=3 SCAR=11.2
Pod
documented
Branch: 2 branches
| |||||||||||||||
| 132 | 99 | +{ map { ($_ => $self->{$_}) } grep !$NONENUM{$_}, keys %$self }; | |||||||||||||
| 133 | 21 | ▶ | } | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 134 | |||||||||||||||
| 135 | 21 | __PACKAGE__->meta->make_immutable(); | |||||||||||||
| 136 | |||||||||||||||
| 137 | 21 | 1; | |||||||||||||