blob: c6a81a13f19b9fafb0834b3853661b16aaa6942e [file] [log] [blame]
Sylvain Munaut76504e02010-12-07 00:24:32 +01001# -*- coding: utf-8 -*-
2
3""" pySim: Exceptions
4"""
5
6#
7# Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com>
Harald Weltee79cc802021-01-21 14:10:43 +01008# Copyright (C) 2021 Harald Welte <laforge@osmocom.org>
Sylvain Munaut76504e02010-12-07 00:24:32 +01009#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program. If not, see <http://www.gnu.org/licenses/>.
22#
23
Harald Weltec91085e2022-02-10 18:05:45 +010024
Denis 'GNUtoo' Carikli8902bcd2019-09-12 15:03:23 +020025class NoCardError(Exception):
Harald Weltec91085e2022-02-10 18:05:45 +010026 """No card was found in the reader."""
27 pass
28
Vadim Yanitskiy1381ff12018-10-27 01:48:15 +070029
Denis 'GNUtoo' Carikli8902bcd2019-09-12 15:03:23 +020030class ProtocolError(Exception):
Harald Weltec91085e2022-02-10 18:05:45 +010031 """Some kind of protocol level error interfacing with the card."""
32 pass
33
Denis 'GNUtoo' Carikli8902bcd2019-09-12 15:03:23 +020034
35class ReaderError(Exception):
Harald Weltec91085e2022-02-10 18:05:45 +010036 """Some kind of general error with the card reader."""
37 pass
38
Harald Weltee79cc802021-01-21 14:10:43 +010039
40class SwMatchError(Exception):
Harald Weltec91085e2022-02-10 18:05:45 +010041 """Raised when an operation specifies an expected SW but the actual SW from
42 the card doesn't match."""
43
44 def __init__(self, sw_actual: str, sw_expected: str, rs=None):
45 """
46 Args:
47 sw_actual : the SW we actually received from the card (4 hex digits)
48 sw_expected : the SW we expected to receive from the card (4 hex digits)
49 rs : interpreter class to convert SW to string
50 """
51 self.sw_actual = sw_actual
52 self.sw_expected = sw_expected
53 self.rs = rs
54
55 def __str__(self):
Harald Welte7416d462022-07-27 18:37:40 +020056 if self.rs and self.rs.lchan[0]:
57 r = self.rs.lchan[0].interpret_sw(self.sw_actual)
Harald Weltec91085e2022-02-10 18:05:45 +010058 if r:
59 return "SW match failed! Expected %s and got %s: %s - %s" % (self.sw_expected, self.sw_actual, r[0], r[1])
60 return "SW match failed! Expected %s and got %s." % (self.sw_expected, self.sw_actual)