forked from OpenKore/openkore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptions.pm
More file actions
89 lines (82 loc) · 3.08 KB
/
Exceptions.pm
File metadata and controls
89 lines (82 loc) · 3.08 KB
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
#########################################################################
# OpenKore - Exception utility functions and common exception objects
#
# Copryight (c) 2006 OpenKore Development Team
#
# This software is open source, licensed under the GNU General Public
# License, version 2.
# Basically, this means that you're allowed to modify and distribute
# this software. However, if you distribute modified versions, you MUST
# also distribute the source code.
# See http://www.gnu.org/licenses/gpl.html for the full license.
#########################################################################
##
# MODULE DESCRIPTION: Exception utility functions and common exception objects.
#
# This module provides some exception utility functions, to be used in combination
# with <a href="http://cpan.uwinnipeg.ca/htdocs/Exception-Class/Exception/Class.html">Exeption::Class</a>.
#
# It also defines the following commonly-used exception objects:
# `l
# - ArgumentException - An invalid argument is passed to a function.
# - ModuleLoadException - A Perl module cannot be loaded.
# - ClassLoadException - A class cannot be created.
# - IOException - Input/output exception occured.
# - FileNotFoundException - A file is not found.
# - SocketException - An error occured during a socket operating, such as connecting to a server.
# - BusNotRunningException - The OpenKore bus system is not running.
# - DataFormatException - The data format is invalid.
# - UTF8MalformedException - Invalid UTF-8 data encountered.
# `l`
package Utils::Exceptions;
use strict;
use Exporter;
use base qw(Exporter);
use Scalar::Util;
use Exception::Class (
'ArgumentException',
'ModuleLoadException' => { fields => 'module' },
'ClassCreateException' => { fields => 'class' },
'IOException',
'FileNotFoundException' => { isa => 'IOException', fields => 'filename' },
'SocketException' => { isa => 'IOException' },
'BusNotRunningException' => { isa => 'IOException' },
'ProtocolException' => { isa => 'IOException' },
'DataFormatException',
'UTF8MalformedException' => { isa => 'DataFormatException', fields => ['textfileline', 'textfile'] }
);
our @EXPORT = qw(caught);
##
# Object caught(class1, [class2, class3, ...])
# classN: The class name of an exception object.
#
# Checks whether the currently caught exception ($@) is one of the types
# specified in the parameters. Returns $@ if it is, undef otherwise. This
# function is allows you to write in try-catch-style syntax.
#
# This symbol is exported by default.
#
# Example:
# eval {
# SomeException->throw(error => "foo");
# };
# if (my $e = caught("SomeException")) {
# print "SomeException caught: " . $e->error . "\n";
# } elsif (my $e = caught("OtherException")) {
# print "OtherException caught: " . $e->error . "\n";
# } elsif (my $e = caught("YetAnotherException1", "YetAnotherException2")) {
# print "Caught YetAnotherException1 or YetAnotherException1.\n";
# } elsif ($@) {
# # Rethrow exception.
# die $@;
# }
sub caught {
my $e = $@;
foreach my $class (@_) {
if (UNIVERSAL::isa($e, $class)) {
return $e;
}
}
return undef;
}
1;