Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Push — master ( b3de90...534bcd )
by Jérémiah
38s
created

ErrorHandler::treatErrors()   C

Complexity

Conditions 7
Paths 6

Size

Total Lines 41
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 41
ccs 25
cts 25
cp 1
rs 6.7272
cc 7
eloc 22
nc 6
nop 2
crap 7
1
<?php
2
3
/*
4
 * This file is part of the OverblogGraphQLBundle package.
5
 *
6
 * (c) Overblog <http://github.com/overblog/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Overblog\GraphQLBundle\Error;
13
14
use GraphQL\Error;
15
use GraphQL\Executor\ExecutionResult;
16
use Psr\Log\LoggerInterface;
17
use Psr\Log\NullLogger;
18
19
class ErrorHandler
20
{
21
    const DEFAULT_ERROR_MESSAGE = 'Internal server Error';
22
23
    /** @var LoggerInterface */
24
    private $logger;
25
26
    /** @var string */
27
    private $internalErrorMessage;
28
29 29
    public function __construct($internalErrorMessage = null, LoggerInterface $logger = null)
30
    {
31 29
        $this->logger = (null === $logger) ? new NullLogger() : $logger;
32 29
        if (empty($internalErrorMessage)) {
33 29
            $internalErrorMessage = self::DEFAULT_ERROR_MESSAGE;
34 29
        }
35 29
        $this->internalErrorMessage = $internalErrorMessage;
36 29
    }
37
38
    /**
39
     * @param Error[] $errors
40
     * @param $throwRawException
41
     *
42
     * @return Error[]
43
     *
44
     * @throws \Exception
45
     */
46 29
    protected function treatErrors(array $errors, $throwRawException)
47
    {
48 29
        $treatedErrors = [];
49
50
        /** @var Error $error */
51 29
        foreach ($errors as $error) {
52 5
            $rawException = $error->getPrevious();
53
54
            // Parse error or user error
55 5
            if (null === $rawException || $rawException instanceof UserError) {
56 4
                $treatedErrors[] = $error;
57 4
                continue;
58
            }
59
60
            // multiple errors
61 2
            if ($rawException instanceof UserErrors) {
62 1
                $rawExceptions = $rawException;
63 1
                foreach ($rawExceptions->getErrors() as $rawException) {
64 1
                    $treatedErrors[] = Error::createLocatedError($rawException, $error->nodes);
65 1
                }
66 1
                continue;
67
            }
68
69
            // if is a try catch exception wrapped in Error
70 2
            if ($throwRawException) {
71 1
                throw $rawException;
72
            }
73
74 1
            $this->logException($rawException);
75
76 1
            $treatedErrors[] = new Error(
77 1
                $this->internalErrorMessage,
78 1
                $error->nodes,
79 1
                $rawException,
80 1
                $error->getSource(),
81 1
                $error->getPositions()
82 1
            );
83 28
        }
84
85 28
        return $treatedErrors;
86
    }
87
88 1
    public function logException(\Exception $exception)
89
    {
90 1
        $message = sprintf(
91 1
            '%s: %s[%d] (uncaught exception) at %s line %s.',
92 1
            get_class($exception),
93 1
            $exception->getMessage(),
94 1
            $exception->getCode(),
95 1
            $exception->getFile(),
96 1
            $exception->getLine()
97 1
        );
98
99 1
        $this->logger->error($message, ['exception' => $exception]);
100 1
    }
101
102 29
    public function handleErrors(ExecutionResult $executionResult, $throwRawException = false)
103
    {
104 29
        $executionResult->errors = $this->treatErrors($executionResult->errors, $throwRawException);
105 28
    }
106
}
107