aboutsummaryrefslogtreecommitdiff
path: root/exceptions.cpp
blob: 35fa37bd2ac81250b3796b75a6d8ffa1d49fc99a (plain)
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
#include <stdlib.h>
#include <iostream>

#include "vmime/vmime.hpp"
#include "vmime/security/cert/certificateIssuerVerificationException.hpp"
#include "vmime/security/cert/unsupportedCertificateTypeException.hpp"
#include "vmime/security/cert/certificateNotTrustedException.hpp"
#include "vmime/security/cert/certificateExpiredException.hpp"
#include "vmime/security/cert/certificateNotYetValidException.hpp"
#include "vmime/exception.hpp"

#include "exceptions.hpp"
#include "utilities.h"

using namespace vmime::exceptions;
using namespace vmime::security::cert;

#define EX_COMPATIBLE(ex_type_name) (dynamic_cast<ex_type_name*>(e) != nullptr)

void handle_vmime_exception(vmime::exception *e)
{
	if (EX_COMPATIBLE(authentication_error))
		FAIL(MSG_BAD_AUTHENTICATION);
	if (EX_COMPATIBLE(certificateIssuerVerificationException))
		FAIL(MSG_CERT_VERIFICATION_FAIL);
	if (EX_COMPATIBLE(unsupportedCertificateTypeException))
		FAIL(MSG_BAD_CERT_FORMAT);
	if (EX_COMPATIBLE(certificateNotTrustedException))
		FAIL(MSG_UNKNOWN_CERT);
	if (EX_COMPATIBLE(certificateExpiredException))
		FAIL(MSG_CERT_EXPIRED);
	if (EX_COMPATIBLE(certificateNotYetValidException))
		FAIL(MSG_CERT_NOT_YET_VALID);

	if (EX_COMPATIBLE(connection_error))
		std::cerr << "Blad polaczenia z serwerem: ";
	else
		std::cerr << "Blad (biblioteka vmime): ";
	std::cerr << e->what() << std::endl;

#define PRINT_MORE(ex_type_name, text, more)				\
	ex_type_name *ex_type_name =					\
		dynamic_cast<vmime::exceptions::ex_type_name*>(e);	\
	if (ex_type_name != nullptr)					\
		std::cerr << text << ex_type_name->more << std::endl;

#define PRINT_RESPONSE(ex_type_name)					\
	PRINT_MORE(ex_type_name, "Odpowiedz serwera: ", response())

	PRINT_RESPONSE(command_error);
	PRINT_RESPONSE(connection_greeting_error);
	PRINT_RESPONSE(authentication_error);
	PRINT_RESPONSE(invalid_response);
	PRINT_MORE(filesystem_exception, "Sciezka: ",
		   path().toString("\\", vmime::charsets::WINDOWS_1250));

	FAIL_NOMSG();
}

void handle_std_exception(std::exception *e)
{
	if (EX_COMPATIBLE(silent_exception))
		FAIL_NOMSG();
	if (EX_COMPATIBLE(std::bad_alloc))
		FAIL(MSG_MALLOC_FAIL);

	std::cerr << "Blad (biblioteka standardowa): "
		  << e->what() << std::endl;

	FAIL_NOMSG();
}