File: ErrorHandling.pm

package info (click to toggle)
otrs2 6.0.32-6
  • links: PTS
  • area: non-free
  • in suites: bullseye
  • size: 197,336 kB
  • sloc: perl: 1,003,018; javascript: 75,060; xml: 70,883; php: 51,819; sql: 22,361; sh: 379; makefile: 51
file content (379 lines) | stat: -rw-r--r-- 13,159 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --

package Kernel::GenericInterface::ErrorHandling;

use strict;
use warnings;

use Kernel::GenericInterface::Debugger;
use Kernel::System::VariableCheck qw(:all);

our @ObjectDependencies = (
    'Kernel::Config',
    'Kernel::System::Log',
    'Kernel::System::Main',
    'Kernel::System::GenericInterface::Webservice',
);

=head1 NAME

Kernel::GenericInterface::ErrorHandling - Error object to execute registered error handler modules

=head1 SYNOPSIS

=head1 PUBLIC INTERFACE

=over 4

=cut

=item new()

create an object. Do not create it directly, instead use:

    use Kernel::System::ObjectManager;
    local $Kernel::OM = Kernel::System::ObjectManager->new();
    my $ErrorObject = $Kernel::OM->Get('Kernel::GenericInterface::ErrorHandling');

=cut

sub new {
    my ( $Type, %Param ) = @_;

    # allocate new hash for object
    my $Self = {};
    bless( $Self, $Type );

    return $Self;
}

=item HandleError()

Receives the current web service and operation or invoker data, as well as the result
of the HandleError method from the related invoker or operation.
The data will be printed via the debugger.
For every registered error handler its configuration will be checked to determine if
it should be called.

    my $Result =  $ErrorObject->HandleError(
        WebserviceID      => 1,                     # ID of the configured remote web service to use
        WebserviceConfig  => $WebserviceConfig,
        CommunicationID   => '02a381c622d5f93df868a42151db1983', # communication ID of current debugger instance
        CommunicationType => 'Requester',           # May be 'Requester' or 'Provider'
        CommunicationName => 'CreateTicket',        # optional, name of Invoker or Operation
        ErrorStage        => 'MappingIn',           # stage where error occurred
        Summary           => $ErrorSummary,
        Data              => $ErrorData,
        PastExecutionData => $PastExecutionDataStructure,   # optional
    );

    $Result = {
        Success      => 0,
        ErrorMessage => $ErrorSummary,              # returns summary from call
    };

=cut

sub HandleError {
    my ( $Self, %Param ) = @_;

    for my $Needed (qw(WebserviceID WebserviceConfig CommunicationID CommunicationType ErrorStage Summary Data)) {
        if ( !$Param{$Needed} ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => "Got no $Needed!",
            );

            return {
                Success      => 0,
                ErrorMessage => "Got no $Needed!",
            };
        }
    }

    # Add error message to debugger.
    my $DebuggerObject = Kernel::GenericInterface::Debugger->new(
        DebuggerConfig    => $Param{WebserviceConfig}->{Debugger},
        WebserviceID      => $Param{WebserviceID},
        CommunicationType => $Param{CommunicationType},
        CommunicationID   => $Param{CommunicationID},
    );

    $DebuggerObject->Error(
        Summary => $Param{Summary},
        Data    => $Param{Data},
    );

    my $ErrorHandlingConfig = $Kernel::OM->Get('Kernel::Config')->Get('GenericInterface::ErrorHandling::Module');

    if ( !IsHashRefWithData($ErrorHandlingConfig) ) {
        return $Self->_LogAndReturn(
            %Param,
            ErrorMessage => $Param{Summary},
        );
    }

    my $ErrorHandlingModules  = $Param{WebserviceConfig}->{ $Param{CommunicationType} }->{ErrorHandling}         || {};
    my $ErrorHandlingPriority = $Param{WebserviceConfig}->{ $Param{CommunicationType} }->{ErrorHandlingPriority} || {};

    # Check for configured error handling modules and priority.
    if ( !IsHashRefWithData($ErrorHandlingModules) || !IsArrayRefWithData($ErrorHandlingPriority) ) {

        # Obviously nothing configured, just return.
        return {
            Success => 1,
        };
    }

    my $MainObject = $Kernel::OM->Get('Kernel::System::Main');

    # Run all registered and activated error handler modules.
    my @ReturnData;
    my $ReScheduleData;
    my %StopAfterMatch;
    my %FilterRegexStrings;
    ERRORHANDLINGCONFIGKEY:
    for my $ErrorHandlingConfigKey ( @{$ErrorHandlingPriority} ) {
        next ERRORHANDLINGCONFIGKEY if !$ErrorHandlingConfigKey;

        if ( !IsHashRefWithData( $ErrorHandlingModules->{$ErrorHandlingConfigKey} ) ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message =>
                    "Error handling module name not found for entry '$ErrorHandlingConfigKey' in WebserviceID $Param{WebserviceID} ($Param{CommunicationType})!",
            );

            next ERRORHANDLINGCONFIGKEY;
        }

        my $ModuleConfig       = $ErrorHandlingModules->{$ErrorHandlingConfigKey};
        my $ModuleRegistration = $ErrorHandlingConfig->{ $ModuleConfig->{Type} };
        next ERRORHANDLINGCONFIGKEY if $StopAfterMatch{ $ModuleConfig->{Type} };

        if ( !IsHashRefWithData($ModuleRegistration) ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message =>
                    "Error handling module config registration not found for entry '$ErrorHandlingConfigKey' in WebserviceID $Param{WebserviceID} ($Param{CommunicationType})!",
            );

            next ERRORHANDLINGCONFIGKEY;
        }

        # Check if the registration for each error handler module is valid.
        if ( !$ModuleRegistration->{Name} ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message =>
                    "Module registration for error handling entry '$ErrorHandlingConfigKey' is invalid in WebserviceID $Param{WebserviceID} ($Param{CommunicationType})!",
            );

            next ERRORHANDLINGCONFIGKEY;
        }

        my $ErrorHandlingModule = 'Kernel::GenericInterface::ErrorHandling::' . $ModuleConfig->{Type};

        # Check if backend field exists.
        if ( !$MainObject->Require($ErrorHandlingModule) ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message =>
                    "Can't load error handling module '$ErrorHandlingModule' in WebserviceID $Param{WebserviceID} ($Param{CommunicationType})!",
            );

            next ERRORHANDLINGCONFIGKEY;
        }

        # Check if error handling object should be called based on filters.
        my $CommunicationNameFilterName;
        if ( $Param{CommunicationType} eq 'Requester' ) {
            $CommunicationNameFilterName = 'InvokerFilter';
        }
        else {
            $CommunicationNameFilterName = 'OperationFilter';
        }

        if ( IsArrayRefWithData( $ModuleConfig->{$CommunicationNameFilterName} ) ) {

            # Only execute module for configured invoker/operations.
            next ERRORHANDLINGCONFIGKEY if !IsStringWithData( $Param{CommunicationName} );
            if ( !grep { $_ eq $Param{CommunicationName} } @{ $ModuleConfig->{$CommunicationNameFilterName} } ) {
                next ERRORHANDLINGCONFIGKEY;
            }
        }

        if ( IsArrayRefWithData( $ModuleConfig->{ErrorStageFilter} ) ) {

            # Only execute module for configured error stages.
            next ERRORHANDLINGCONFIGKEY if !grep { $_ eq $Param{ErrorStage} } @{ $ModuleConfig->{ErrorStageFilter} };
        }

        if ( IsStringWithData( $ModuleConfig->{ErrorMessageContentFilter} ) ) {

            # OPrepare filter strings.
            my @FilterParams = (qw(Summary Data));
            if ( !%FilterRegexStrings ) {
                for my $FilterParam (@FilterParams) {
                    if ( IsString( $Param{$FilterParam} ) ) {
                        $FilterRegexStrings{$FilterParam} = $Param{$FilterParam};
                    }
                    else {
                        $FilterRegexStrings{$FilterParam} = $MainObject->Dump( $Param{$FilterParam} );
                    }
                }
            }

            # Only execute module if configured regex contains any matches.
            my $RegexMatch;
            my $FilterRegex = qr{$ModuleConfig->{ErrorMessageContentFilter}};
            FILTERPARAM:
            for my $FilterParam (@FilterParams) {
                next FILTERPARAM if $FilterRegexStrings{$FilterParam} !~ $FilterRegex;
                $RegexMatch = 1;
                last FILTERPARAM;
            }

            next ERRORHANDLINGCONFIGKEY if !$RegexMatch;
        }

        my $ErrorHandlingObject = $Kernel::OM->Get($ErrorHandlingModule);
        if ( !$ErrorHandlingObject ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message =>
                    "Couldn't create an object of error handling module '$ModuleConfig->{Type}' in WebserviceID $Param{WebserviceID} ($Param{CommunicationType})!",
            );

            next ERRORHANDLINGCONFIGKEY;
        }

        if ( ref $ErrorHandlingObject ne $ErrorHandlingModule ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message =>
                    "Error handling object for module '$ModuleConfig->{Type}' was not created successfully in WebserviceID $Param{WebserviceID} ($Param{CommunicationType})!",
            );

            next ERRORHANDLINGCONFIGKEY;
        }

        my $ErrorHandlingResult = $ErrorHandlingObject->Run(
            %Param,
            ModuleConfig => $ModuleConfig,
        );
        if ( !$ErrorHandlingResult->{Success} ) {
            return $Self->_LogAndReturn(
                %Param,
                ErrorMessage => $ErrorHandlingResult->{ErrorMessage},
            );
        }

        push @ReturnData, {
            ModuleConfig => $ModuleConfig,
            ModuleData   => $ErrorHandlingResult->{Data},
            ModuleName   => $ErrorHandlingConfigKey,
        };

        $DebuggerObject->Debug(
            Summary => "Executed error handling module '$ErrorHandlingConfigKey'",
            Data    => $ReturnData[-1],
        );

        if ( IsHashRefWithData( $ErrorHandlingResult->{ReScheduleData} ) ) {
            $ReScheduleData = $ErrorHandlingResult->{ReScheduleData};

            # Print desired reschedule result.
            $DebuggerObject->Info(
                Summary => "Got reschedule decision from error handling '$ErrorHandlingConfigKey'",
                Data    => $ErrorHandlingResult->{ReScheduleData},
            );
        }

        # Check if we should skip some/all further modules.
        if ( IsStringWithData( $ModuleConfig->{StopAfterMatch} ) ) {
            last ERRORHANDLINGCONFIGKEY if $ModuleConfig->{StopAfterMatch} eq 'all';
            if ( $ModuleConfig->{StopAfterMatch} eq 'backend' ) {
                $StopAfterMatch{ $ModuleConfig->{Type} } = 1;
            }
        }

        next ERRORHANDLINGCONFIGKEY if !IsHashRefWithData( $ErrorHandlingResult->{Data} );
    }

    # Print final rescheduling info.
    if (
        IsHashRefWithData($ReScheduleData)
        && $ReScheduleData->{ReSchedule}
        )
    {
        $DebuggerObject->Notice(
            Summary => 'Request will be retried again at:',
            Data    => $ReScheduleData->{ExecutionTime},
        );
    }

    return {
        Success        => 1,
        Data           => \@ReturnData,
        ReScheduleData => $ReScheduleData,
    };
}

sub _LogAndReturn {
    my ( $Self, %Param ) = @_;

    for my $Needed (qw(WebserviceID WebserviceConfig CommunicationID CommunicationType Summary ErrorMessage)) {
        if ( !$Param{$Needed} ) {
            $Kernel::OM->Get('Kernel::System::Log')->Log(
                Priority => 'error',
                Message  => "Got no $Needed!",
            );

            return {
                Success      => 0,
                ErrorMessage => $Param{Summary} || "Got no $Needed!",
            };
        }
    }

    my $DebuggerObject = Kernel::GenericInterface::Debugger->new(
        DebuggerConfig    => $Param{WebserviceConfig}->{Debugger},
        WebserviceID      => $Param{WebserviceID},
        CommunicationType => $Param{CommunicationType},
        CommunicationID   => $Param{CommunicationID},
    );

    $DebuggerObject->Error(
        Summary => $Param{ErrorMessage},
    );

    $Kernel::OM->Get('Kernel::System::Log')->Log(
        Priority => 'error',
        Message  => $Param{ErrorMessage},
    );

    return {
        Success      => 0,
        ErrorMessage => $Param{ErrorMessage},
    };
}

1;

=back

=head1 TERMS AND CONDITIONS

This software is part of the OTRS project (L<https://otrs.org/>).

This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (GPL). If you
did not receive this file, see L<https://www.gnu.org/licenses/gpl-3.0.txt>.

=cut