Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2020 YADRO |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 3 | |
| 4 | #include "zlib_exception.hpp" |
| 5 | |
| 6 | #include <cstring> |
| 7 | |
| 8 | ZlibException::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 Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 38 | errDesc = "Unable to "; |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 39 | switch (op) |
| 40 | { |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 41 | case create: |
| 42 | errDesc += "create"; |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 43 | break; |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 44 | case close: |
| 45 | errDesc += "close"; |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 46 | break; |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 47 | case write: |
| 48 | errDesc += "write"; |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 49 | break; |
| 50 | } |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 51 | errDesc += " file "; |
| 52 | errDesc += fileName; |
| 53 | errDesc += ": "; |
| 54 | errDesc += details; |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | const char* ZlibException::what() const noexcept |
| 58 | { |
Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 59 | return errDesc.c_str(); |
Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 60 | } |