File: Error.pm

package info (click to toggle)
libgraphql-perl 0.54-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 712 kB
  • sloc: perl: 5,094; makefile: 2
file content (137 lines) | stat: -rw-r--r-- 2,654 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package GraphQL::Error;

use 5.014;
use strict;
use warnings;
use Moo;
use Types::Standard -all;
use GraphQL::Type::Library -all;
use GraphQL::MaybeTypeCheck;
use GraphQL::Debug qw(_debug);

our $VERSION = '0.02';

use constant DEBUG => $ENV{GRAPHQL_DEBUG};
my %NONENUM = map { ($_ => 1) } qw(original_error);
use overload '""' => 'to_string';

=head1 NAME

GraphQL::Error - GraphQL error object

=head1 SYNOPSIS

  use GraphQL::Error;
  die GraphQL::Error->new(message => 'Something is not right...');

=head1 DESCRIPTION

Class implementing GraphQL error object.

=head1 ATTRIBUTES

=head2 message

=cut

has message => (is => 'ro', isa => Str, required => 1);

=head2 original_error

If there is an original error to be preserved.

=cut

has original_error => (is => 'ro', isa => Any);

=head2 locations

Array-ref of L<GraphQL::Type::Library/DocumentLocation>s.

=cut

has locations => (is => 'ro', isa => ArrayRef[DocumentLocation]);

=head2 path

Array-ref of L<GraphQL::Type::Library/StrNameValid>s or C<Int>s describing
the path from the top operation (being either fields, or a List offset).

=cut

has path => (is => 'ro', isa => ArrayRef[StrNameValid | Int]);

=head2 extensions

Hash-ref of L<GraphQL::Type::Library/JSONable>s providing additional
information.

=cut

has extensions => (is => 'ro', isa => Optional[HashRef[JSONable]]);

=head1 METHODS

=head2 is

Is the supplied scalar an error object?

=cut

method is(Any $item) :ReturnType(Bool) { ref $item eq __PACKAGE__ }

=head2 coerce

If supplied scalar is an error object, return. If not, return one with
it as message. If an object, message will be stringified version of that,
it will be preserved as C<original_error>.

=cut

method coerce(
  Any $item
) :ReturnType(InstanceOf[__PACKAGE__]) {
  DEBUG and _debug('Error.coerce', $item);
  return $item if __PACKAGE__->is($item);
  $item ||= 'Unknown error';
  !is_Str($item)
    ? $self->new(message => $item.'', original_error => $item)
    : $self->new(message => $item);
}

=head2 but

Returns a copy of the error object, but with the given properties (as
with a C<new> method, not coincidentally) overriding the existing ones.

=cut

sub but :ReturnType(InstanceOf[__PACKAGE__]) {
  my $self = shift;
  $self->new(%$self, @_);
}

=head2 to_string

Converts to string.

=cut

method to_string(@ignore) :ReturnType(Str) {
  $self->message;
}

=head2 to_json

Converts to a JSON-able hash, in the format to send back as a member of
the C<errors> array in the results.

=cut

method to_json() :ReturnType(HashRef) {
  +{ map { ($_ => $self->{$_}) } grep !$NONENUM{$_}, keys %$self };
}

__PACKAGE__->meta->make_immutable();

1;