3 SGN::Exception - expressive exception object for web applications
7 die SGN::Exception->new(
9 title => 'Froozle Error',
10 public_message => "Cannot process the froozle.",
11 developer_message => "Froozle was '$froozle', path was '$fogbat_path'.",
17 # developers need to be notified that this happened
24 The SGN::Exception object is meant to hold a wide variety of
25 information about the nature of an exception or error condition.
26 Nearly all the attributes of the exception object are optional.
30 package SGN
::Exception
;
34 # make catalyst use this exception class
36 $Catalyst::Exception
::CATALYST_EXCEPTION_CLASS
= __PACKAGE__
;
39 with
'Catalyst::Exception::Basic';
49 All attributes are read-only. None are required.
53 String, publicly-visible message for this error. Should not reveal
54 details that could have security implications.
58 has
'public_message' => (
63 =head2 developer_message
65 String, private message for developers. Should be included in error
66 notifications sent to developers, debug backtraces, etc.
70 has
'developer_message' => (
75 #TODO: redundant, deleteme
76 has
'explanation' => (
83 Optional string, user-visible title for the exception as presented to the user.
92 =head2 is_server_error
94 Boolean, true if this error indicates a problem on the server side.
96 Defaults to true most of the time, but defaults to false if
97 is_client_error is explicitly set to true.
101 has
'is_server_error' => (
107 =head2 is_client_error
109 Boolean, true if this error indicates a problem on the client side.
111 Defaults to false most of the time, but default to true if
112 is_server_error is set and false.
116 has
'is_client_error' => (
124 Explicitly set the status code of the HTTP response for this error
127 Defaults to 500 (Internal Server Error) if is_server_error is set,
128 else 400 (Bad Request) if is_client_error is set, or 200 (OK) if
129 is_client_error and is_server_error are both false.
133 has
'http_status' => (
138 sub _build_http_status
{
140 $self->is_server_error ?
500 :
141 $self->is_client_error ?
400 :
147 Flag indicating whether developers should be actively notified that
148 this exception occurred.
150 Boolean, defaults to true if is_server_error is set, false otherwise.
158 ); sub _build_notify
{
159 shift->is_server_error
162 around
'BUILDARGS' => sub {
165 my %args = @_ > 1 ?
@_ : ( message
=> @_ );
167 $args{developer_message
} ||= $args{message
};
168 $args{message
} ||= $args{developer_message
} || $args{public_message
} || '(no message)';
170 if( defined $args{is_client_error
} && !$args{is_client_error
} ) {
171 $args{is_server_error
} = 1 unless defined $args{is_server_error
};
173 if( defined $args{is_server_error
} && !$args{is_server_error
} ) {
174 $args{is_client_error
} = 1 unless defined $args{is_client_error
};
176 if( $args{is_client_error
} && ! defined $args{is_server_error
} ) {
177 $args{is_server_error
} = 0;
180 return $class->$orig( %args );
187 Return a plaintext string representation of this exception, suitable
188 for display in consoles and so forth.
195 ($self->public_message || '') . "\n"
196 .'Developer message: '
197 .($self->developer_message || 'none');
200 __PACKAGE__
->meta->make_immutable;