Passed
Push — master ( d5a095...990124 )
by Vincent
04:15 queued 12s
created

handle(AdminPerformer,Throwable)   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
eloc 5
1
/*
2
 * This file is part of Araknemu.
3
 *
4
 * Araknemu is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * Araknemu is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with Araknemu.  If not, see <https://www.gnu.org/licenses/>.
16
 *
17
 * Copyright (c) 2017-2019 Vincent Quatrevieux
18
 */
19
20
package fr.quatrevieux.araknemu.game.admin.exception;
21
22
import fr.quatrevieux.araknemu.game.admin.AdminPerformer;
23
24
import java.util.HashMap;
25
import java.util.Map;
26
27
/**
28
 * Handle exceptions
29
 */
30
public final class ExceptionHandler {
31 1
    private final Map<Class, Function> handlers = new HashMap<>();
32
33 1
    public ExceptionHandler() {
34 1
        register(CommandNotFoundException.class, (user, e) -> user.error("Command '{}' is not found", e.command()));
35 1
        register(CommandException.class, (user, e) -> user.error("An error occurs during execution of '{}' : {}", e.command(), e.getMessage()));
36 1
        register(CommandPermissionsException.class, (user, e) -> user.error("Unauthorized command '{}', you need at least these permissions {}", e.command(), e.permissions()));
37 1
        register(ContextException.class, (user, e) -> user.error("Error during resolving context : {}", e.getMessage()));
38 1
        register(ContextNotFoundException.class, (user, e) -> user.error("The context '{}' is not found", e.context()));
39 1
    }
40
41
    /**
42
     * Handle the error
43
     */
44
    public void handle(AdminPerformer performer, Throwable error) {
45 1
        if (!handlers.containsKey(error.getClass())) {
46 1
            performer.error("Error : {}", error.toString());
47 1
            return;
48
        }
49
50 1
        handlers.get(error.getClass()).handle(performer, error);
51 1
    }
52
53
    private <T extends Throwable> void register(Class<T> type, Function<T> handler) {
54 1
        handlers.put(type, handler);
55 1
    }
56
57
    @FunctionalInterface
58
    public interface Function<T extends Throwable> {
59
        /**
60
         * Handle the exception
61
         *
62
         * @param performer The command performer
63
         * @param error The triggered error
64
         */
65
        public void handle(AdminPerformer performer, T error);
66
    }
67
}
68