| 12
 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
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 
 | # $Id: Exception.pm 30 2008-08-16 00:58:07Z dtikhonov $
#
# We are going to throw exceptions, because we're cool like that.
package RT::Client::REST::Exception;
use base qw(Exception::Class);
use strict;
use warnings;
use vars qw($VERSION);
$VERSION = '0.19';
use Error;
use Exception::Class (
    'RT::Client::REST::OddNumberOfArgumentsException'   => {
        isa         => __PACKAGE__,
        description => "This means that we wanted name/value pairs",
    },
    'RT::Client::REST::InvaildObjectTypeException'   => {
        isa         => __PACKAGE__,
        description => "Invalid object type was specified",
    },
    'RT::Client::REST::MalformedRTResponseException'    => {
        isa         => __PACKAGE__,
        description => "Malformed RT response received from server",
    },
    'RT::Client::REST::InvalidParameterValueException'  => {
        isa         => __PACKAGE__,
        description => "This happens when you feed me bad values",
    },
    'RT::Client::REST::CannotReadAttachmentException'  => {
        isa         => __PACKAGE__,
        description => "Cannot read attachment",
    },
    'RT::Client::REST::RequiredAttributeUnsetException' => {
        isa         => __PACKAGE__,
        description => "An operation failed because a required attribute " .
                       "was not set in the object",
    },
    'RT::Client::REST::RTException' => {
        isa         => __PACKAGE__,
        fields      => ['code'],
        description => "RT server returned an error code",
    },
    'RT::Client::REST::ObjectNotFoundException' => {
        isa         => 'RT::Client::REST::RTException',
        description => 'One or more of the specified objects was not found',
    },
    'RT::Client::REST::CouldNotCreateObjectException' => {
        isa         => 'RT::Client::REST::RTException',
        description => 'Object could not be created',
    },
    'RT::Client::REST::AuthenticationFailureException'  => {
        isa         => 'RT::Client::REST::RTException',
        description => "Incorrect username or password",
    },
    'RT::Client::REST::UpdateException' => {
        isa         => 'RT::Client::REST::RTException',
        description => 'Error updating an object.  Virtual exception',
    },
    'RT::Client::REST::UnknownCustomFieldException' => {
        isa         => 'RT::Client::REST::RTException',
        description => 'Unknown custom field',
    },
    'RT::Client::REST::InvalidQueryException' => {
        isa         => 'RT::Client::REST::RTException',
        description => 'Invalid query (server could not parse it)',
    },
    'RT::Client::REST::CouldNotSetAttributeException' => {
        isa         => 'RT::Client::REST::UpdateException',
        description => 'Attribute could not be updated with a new value',
    },
    'RT::Client::REST::InvalidEmailAddressException' => {
        isa         => 'RT::Client::REST::UpdateException',
        description => 'Invalid e-mail address',
    },
    'RT::Client::REST::AlreadyCurrentValueException' => {
        isa         => 'RT::Client::REST::UpdateException',
        description => 'The attribute you are trying to update already has '.
                       'this value',
    },
    'RT::Client::REST::ImmutableFieldException' => {
        isa         => 'RT::Client::REST::UpdateException',
        description => 'Trying to update an immutable field',
    },
    'RT::Client::REST::IllegalValueException' => {
        isa         => 'RT::Client::REST::UpdateException',
        description => 'Illegal value',
    },
    'RT::Client::REST::UnauthorizedActionException' => {
        isa         => 'RT::Client::REST::RTException',
        description => 'You are not authorized to perform this action',
    },
    'RT::Client::REST::AlreadyTicketOwnerException' => {
        isa         => 'RT::Client::REST::RTException',
        description => 'The owner you are trying to assign to a ticket ' .
            'is already the owner',
    },
    'RT::Client::REST::RequestTimedOutException' => {
        isa         => 'RT::Client::REST::RTException',
        description => 'Request timed out',
    },
    'RT::Client::REST::UnknownRTException' => {
        isa         => 'RT::Client::REST::RTException',
        description => 'Some other RT error',
    },
    'RT::Client::REST::HTTPException'   => {
        isa         => __PACKAGE__,
        fields      => ['code'],
        description => "Error in the underlying protocol (HTTP)",
    },
);
sub _get_exception_class {
    my ($self, $content) = @_;
    if ($content =~ /not found|\d+ does not exist|[Ii]nvalid attachment id/) {
        return 'RT::Client::REST::ObjectNotFoundException';
    } elsif ($content =~ /not create/) {
        return 'RT::Client::REST::CouldNotCreateObjectException';
    } elsif ($content =~ /[Uu]nknown custom field/) {
        return 'RT::Client::REST::UnknownCustomFieldException';
    } elsif ($content =~ /[Ii]nvalid query/) {
        return 'RT::Client::REST::InvalidQueryException';
    } elsif ($content =~ /could not be set to/) {
        return 'RT::Client::REST::CouldNotSetAttributeException';
    } elsif ($content =~ /not a valid email address/) {
        return 'RT::Client::REST::InvalidEmailAddressException';
    } elsif ($content =~ /is already the current value/) {
        return 'RT::Client::REST::AlreadyCurrentValueException';
    } elsif ($content =~ /[Ii]mmutable field/) {
        return 'RT::Client::REST::ImmutableFieldException';
    } elsif ($content =~ /[Ii]llegal value/) {
        return 'RT::Client::REST::IllegalValueException';
    } elsif ($content =~ /[Yy]ou are not allowed/) {
        return 'RT::Client::REST::UnauthorizedActionException';
    } elsif ($content =~ /[Yy]ou already own this ticket/ ||
             $content =~ /[Tt]hat user already owns that ticket/)
    {
        return 'RT::Client::REST::AlreadyTicketOwnerException';
    } else {
        return 'RT::Client::REST::UnknownRTException';
    }
}
sub _rt_content_to_exception {
    my ($self, $content) = @_;
    (my $message = $content) =~ s/^#\s*//;
    chomp($message);
    return $self->_get_exception_class($content)->new(
        message => $message,
    );
}
# Some mildly weird magic to fix up inheritance (see Exception::Class POD).
{
    no strict 'refs';
    push @{__PACKAGE__ . '::ISA'}, 'Exception::Class::Base';
    push @Exception::Class::Base::ISA, 'Error'
        unless Exception::Class::Base->isa('Error');
}
1;
__END__
=head1 NAME
RT::Client::REST::Exception -- exceptions thrown by RT::Client::REST
methods.
=head1 DESCRIPTION
These are exceptions that are thrown by various L<RT::Client::REST>
methods.
=head1 EXCEPTION HIERARCHY
=over 2
=item B<RT::Client::REST::Exception>
This exception is virtual -- it is never thrown.  It is used to group
all the exceptions in this category.
=over 2
=item B<RT::Client::REST::OddNumberOfArgumentsException>
This means that the method you called wants key-value pairs.
=item B<RT::Client::REST::InvaildObjectTypeException>
Thrown when you specify an invalid type to C<show()>, C<edit()>, or
C<search()> methods.
=item B<RT::Client::REST::RequiredAttributeUnsetException>
An operation failed because a required attribute was not set in the object.
=item B<RT::Client::REST::MalformedRTResponseException>
RT server sent response that we cannot parse.  This may very well mean
a bug in this client, so if you get this exception, some debug information
mailed to the author would be appreciated.
=item B<RT::Client::REST::InvalidParameterValueException>
Invalid value for comments, link types, object IDs, etc.
=item B<RT::Client::REST::CannotReadAttachmentException>
Cannot read attachment (thrown from methods "comment()" and "correspond").
=item B<RT::Client::REST::RTException>
This is a virtual exception and is never thrown.  It is used to group
exceptions thrown because RT server returns an error.
=over 2
=item B<RT::Client::REST::ObjectNotFoundException>
One or more of the specified objects was not found.
=item B<RT::Client::REST::AuthenticationFailureException>
Incorrect username or password.
=item B<RT::Client::REST::UpdateException>
This is a virtual exception.  It is used to group exceptions thrown when
RT server returns an error trying to update an object.
=over 2
=item B<RT::Client::REST::CouldNotSetAttributeException>
For one or another reason, attribute could not be updated with the new
value.
=item B<RT::Client::REST::InvalidEmailAddressException>
Invalid e-mail address specified.
=item B<RT::Client::REST::AlreadyCurrentValueException>
The attribute you are trying to update already has this value.  I do not
know why RT insists on treating this as an exception, but since it does so,
so should the client.  You can probably safely catch and throw away this
exception in your code.
=item B<RT::Client::REST::ImmutableFieldException>
Trying to update an immutable field (such as "last_updated", for
example).
=item B<RT::Client::REST::IllegalValueException>
Illegal value for attribute was specified.
=back
=item B<RT::Client::REST::UnknownCustomFieldException>
Unknown custom field was specified in the request.
=item B<RT::Client::REST::InvalidQueryException>
Server could not parse the search query.
=item B<RT::Client::REST::UnauthorizedActionException>
You are not authorized to perform this action.
=item B<RT::Client::REST::AlreadyTicketOwnerException>
The owner you are trying to assign to a ticket is already the owner.
This exception is usually thrown by methods C<take()>, C<untake>, and
C<steal>, if the operation is a noop.
=item B<RT::Client::REST::RequestTimedOutException>
Request timed out.
=item B<RT::Client::REST::UnknownRTException>
Some other RT exception that the driver cannot recognize.
=back
=back
=back
=head1 METHODS
=over 2
=item B<_get_exception_class>
Figure out exception class based on content returned by RT.
=item B<_rt_content_to_exception>
Translate error string returned by RT server into an exception object
ready to be thrown.
=back
=head1 SEE ALSO
L<Exception::Class>,
L<RT::Client::REST>.
=head1 AUTHOR
Dmitri Tikhonov <dtikhonov@yahoo.com>
=cut
 |