Artem Senichev | afc7373 | 2020-05-09 19:04:51 +0300 | [diff] [blame] | 1 | /** |
| 2 | * @brief zLib exception. |
| 3 | * |
| 4 | * This file is part of HostLogger project. |
| 5 | * |
| 6 | * Copyright (c) 2020 YADRO |
| 7 | * |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | */ |
| 20 | |
| 21 | #include "zlib_exception.hpp" |
| 22 | |
| 23 | #include <cstring> |
| 24 | |
| 25 | ZlibException::ZlibException(Operation op, int code, gzFile fd, |
| 26 | const std::string& fileName) |
| 27 | { |
| 28 | std::string details; |
| 29 | if (code == Z_ERRNO) |
| 30 | { |
| 31 | // System error |
| 32 | const int errCode = errno ? errno : EIO; |
| 33 | details = strerror(errCode); |
| 34 | } |
| 35 | else if (fd != Z_NULL) |
| 36 | { |
| 37 | // Try to get description from zLib |
| 38 | int lastErrCode = 0; |
| 39 | const char* lastErrDesc = gzerror(fd, &lastErrCode); |
| 40 | if (lastErrCode) |
| 41 | { |
| 42 | details = '['; |
| 43 | details += std::to_string(lastErrCode); |
| 44 | details += "] "; |
| 45 | details += lastErrDesc; |
| 46 | } |
| 47 | } |
| 48 | if (details.empty()) |
| 49 | { |
| 50 | details = "Internal zlib error (code "; |
| 51 | details += std::to_string(code); |
| 52 | details += ')'; |
| 53 | } |
| 54 | |
| 55 | what_ = "Unable to "; |
| 56 | switch (op) |
| 57 | { |
| 58 | case Create: |
| 59 | what_ += "create"; |
| 60 | break; |
| 61 | case Close: |
| 62 | what_ += "close"; |
| 63 | break; |
| 64 | case Write: |
| 65 | what_ += "write"; |
| 66 | break; |
| 67 | } |
| 68 | what_ += " file "; |
| 69 | what_ += fileName; |
| 70 | what_ += ": "; |
| 71 | what_ += details; |
| 72 | } |
| 73 | |
| 74 | const char* ZlibException::what() const noexcept |
| 75 | { |
| 76 | return what_.c_str(); |
| 77 | } |