blob: 1769700edc58555fe971311fb997def37d371dc6 [file] [log] [blame]
Artem Senicheve8837d52020-06-07 11:59:04 +03001// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2020 YADRO
Artem Senichevafc73732020-05-09 19:04:51 +03003
4#include "zlib_exception.hpp"
5
6#include <cstring>
7
8ZlibException::ZlibException(Operation op, int code, gzFile fd,
9 const std::string& fileName)
10{
11 std::string details;
12 if (code == Z_ERRNO)
13 {
14 // System error
15 const int errCode = errno ? errno : EIO;
16 details = strerror(errCode);
17 }
18 else if (fd != Z_NULL)
19 {
20 // Try to get description from zLib
21 int lastErrCode = 0;
22 const char* lastErrDesc = gzerror(fd, &lastErrCode);
23 if (lastErrCode)
24 {
25 details = '[';
26 details += std::to_string(lastErrCode);
27 details += "] ";
28 details += lastErrDesc;
29 }
30 }
31 if (details.empty())
32 {
33 details = "Internal zlib error (code ";
34 details += std::to_string(code);
35 details += ')';
36 }
37
Artem Senicheve8837d52020-06-07 11:59:04 +030038 errDesc = "Unable to ";
Artem Senichevafc73732020-05-09 19:04:51 +030039 switch (op)
40 {
Artem Senicheve8837d52020-06-07 11:59:04 +030041 case create:
42 errDesc += "create";
Artem Senichevafc73732020-05-09 19:04:51 +030043 break;
Artem Senicheve8837d52020-06-07 11:59:04 +030044 case close:
45 errDesc += "close";
Artem Senichevafc73732020-05-09 19:04:51 +030046 break;
Artem Senicheve8837d52020-06-07 11:59:04 +030047 case write:
48 errDesc += "write";
Artem Senichevafc73732020-05-09 19:04:51 +030049 break;
50 }
Artem Senicheve8837d52020-06-07 11:59:04 +030051 errDesc += " file ";
52 errDesc += fileName;
53 errDesc += ": ";
54 errDesc += details;
Artem Senichevafc73732020-05-09 19:04:51 +030055}
56
57const char* ZlibException::what() const noexcept
58{
Artem Senicheve8837d52020-06-07 11:59:04 +030059 return errDesc.c_str();
Artem Senichevafc73732020-05-09 19:04:51 +030060}