File: exception-py.cpp

package info (click to toggle)
libdnf 0.75.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,468 kB
  • sloc: cpp: 48,297; xml: 1,638; python: 1,537; ansic: 1,223; sql: 227; sh: 54; makefile: 39
file content (130 lines) | stat: -rw-r--r-- 3,775 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2012-2013 Red Hat, Inc.
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <Python.h>

// hawkey
#include "dnf-types.h"

// pyhawkey
#include "exception-py.hpp"

PyObject *HyExc_Exception = NULL;
PyObject *HyExc_Value = NULL;
PyObject *HyExc_Query = NULL;
PyObject *HyExc_Arch = NULL;
PyObject *HyExc_Runtime = NULL;
PyObject *HyExc_Validation = NULL;

int
init_exceptions(void)
{
     HyExc_Exception = PyErr_NewException((char*) "_hawkey.Exception", NULL, NULL);
     if (!HyExc_Exception)
         return 0;

     HyExc_Value = PyErr_NewException((char*) "_hawkey.ValueException", HyExc_Exception,
                                      NULL);
     if (!HyExc_Value)
         return 0;

     HyExc_Query = PyErr_NewException((char*) "_hawkey.QueryException", HyExc_Value,
                                      NULL);
     if (!HyExc_Query)
         return 0;

     HyExc_Arch = PyErr_NewException((char*) "_hawkey.ArchException", HyExc_Value,
                                      NULL);
     if (!HyExc_Arch)
         return 0;

     HyExc_Runtime = PyErr_NewException((char*) "_hawkey.RuntimeException",
                                        HyExc_Exception, NULL);
     if (!HyExc_Runtime)
         return 0;

     HyExc_Validation = PyErr_NewException((char*) "_hawkey.ValidationException",
                                        HyExc_Exception, NULL);
     if (!HyExc_Validation)
         return 0;

     return 1;
}

int
ret2e(int ret, const char *msg)
{
    PyObject *exctype = NULL;
    switch (ret) {
    case 0:
        return 0;
    case DNF_ERROR_FAILED:
        exctype = HyExc_Runtime;
        break;
    case DNF_ERROR_FILE_INVALID: {
        exctype = PyExc_IOError;
        break;
    }
    case DNF_ERROR_INTERNAL_ERROR:
    case DNF_ERROR_BAD_SELECTOR:
        exctype = HyExc_Value;
        break;
    default:
        // try to end it quickly
        assert(0);
        // or fallback to an internal-error exception
        PyErr_SetString(PyExc_AssertionError, msg);
        return 1;
    }

    assert(exctype);
    PyErr_SetString(exctype, msg);
    return 1;
}


PyObject *
op_error2exc(const GError *error)
{
    if (error == NULL)
        Py_RETURN_NONE;

    switch (error->code) {
    case DNF_ERROR_BAD_SELECTOR:
        PyErr_SetString(HyExc_Value,
                        "Ill-formed Selector used for the operation.");
        return NULL;
    case DNF_ERROR_INVALID_ARCHITECTURE:
        PyErr_SetString(HyExc_Arch, "Used arch is unknown.");
        return NULL;
    case DNF_ERROR_PACKAGE_NOT_FOUND:
        PyErr_SetString(HyExc_Validation, "The validation check has failed.");
        return NULL;
    case DNF_ERROR_FILE_INVALID:
        PyErr_SetString(PyExc_IOError, error->message);
        return NULL;
    case DNF_ERROR_CANNOT_WRITE_CACHE:
        PyErr_SetString(PyExc_IOError, "Failed writing the cache.");
        return NULL;
    default:
        PyErr_SetString(HyExc_Exception, error->message);
        return NULL;
    }
}