lib/zlib/Error: derive from std::system_error

This commit is contained in:
Max Kellermann 2024-06-14 16:54:12 +02:00 committed by Max Kellermann
parent 432bfa15f4
commit a739eefb01
5 changed files with 22 additions and 27 deletions

View File

@ -1,12 +0,0 @@
// SPDX-License-Identifier: BSD-2-Clause
// author: Max Kellermann <max.kellermann@gmail.com>
#include "Error.hxx"
#include <zlib.h>
const char *
ZlibError::what() const noexcept
{
return zError(code);
}

View File

@ -3,17 +3,25 @@
#pragma once #pragma once
#include <exception> #include <zlib.h>
class ZlibError final : public std::exception { #include <system_error>
int code;
class ZlibErrorCategory final : public std::error_category {
public: public:
explicit ZlibError(int _code) noexcept:code(_code) {} const char *name() const noexcept override {
return "zlib";
int GetCode() const noexcept {
return code;
} }
const char *what() const noexcept override; std::string message(int condition) const override {
return zError(condition);
}
}; };
inline ZlibErrorCategory zlib_error_category;
inline std::system_error
MakeZlibError(int code, const char *msg) noexcept
{
return std::system_error(code, zlib_error_category, msg);
}

View File

@ -15,7 +15,7 @@ GunzipReader::GunzipReader(Reader &_next)
int result = inflateInit2(&z, 16 + MAX_WBITS); int result = inflateInit2(&z, 16 + MAX_WBITS);
if (result != Z_OK) if (result != Z_OK)
throw ZlibError(result); throw MakeZlibError(result, "inflateInit2() failed");
} }
inline bool inline bool
@ -60,7 +60,7 @@ GunzipReader::Read(std::span<std::byte> dest)
eof = true; eof = true;
return dest.size() - z.avail_out; return dest.size() - z.avail_out;
} else if (result != Z_OK) } else if (result != Z_OK)
throw ZlibError(result); throw MakeZlibError(result, "inflate() failed");
buffer.Consume(r.size() - z.avail_in); buffer.Consume(r.size() - z.avail_in);

View File

@ -20,7 +20,7 @@ GzipOutputStream::GzipOutputStream(OutputStream &_next)
windowBits | gzip_encoding, windowBits | gzip_encoding,
8, Z_DEFAULT_STRATEGY); 8, Z_DEFAULT_STRATEGY);
if (result != Z_OK) if (result != Z_OK)
throw ZlibError(result); throw MakeZlibError(result, "deflateInit2() failed");
} }
GzipOutputStream::~GzipOutputStream() noexcept GzipOutputStream::~GzipOutputStream() noexcept
@ -42,7 +42,7 @@ GzipOutputStream::SyncFlush()
int result = deflate(&z, Z_SYNC_FLUSH); int result = deflate(&z, Z_SYNC_FLUSH);
if (result != Z_OK) if (result != Z_OK)
throw ZlibError(result); throw MakeZlibError(result, "deflate() failed");
if (z.next_out == output) if (z.next_out == output)
break; break;
@ -70,7 +70,7 @@ GzipOutputStream::Finish()
if (result == Z_STREAM_END) if (result == Z_STREAM_END)
break; break;
else if (result != Z_OK) else if (result != Z_OK)
throw ZlibError(result); throw MakeZlibError(result, "deflate() failed");
} }
} }
@ -90,7 +90,7 @@ GzipOutputStream::Write(std::span<const std::byte> src)
int result = deflate(&z, Z_NO_FLUSH); int result = deflate(&z, Z_NO_FLUSH);
if (result != Z_OK) if (result != Z_OK)
throw ZlibError(result); throw MakeZlibError(result, "deflate() failed");
if (z.next_out > output) if (z.next_out > output)
next.Write(std::as_bytes(std::span{output}.first(z.next_out - output))); next.Write(std::as_bytes(std::span{output}.first(z.next_out - output)));

View File

@ -6,7 +6,6 @@ endif
zlib = static_library( zlib = static_library(
'zlib', 'zlib',
'Error.cxx',
'GunzipReader.cxx', 'GunzipReader.cxx',
'GzipOutputStream.cxx', 'GzipOutputStream.cxx',
'AutoGunzipReader.cxx', 'AutoGunzipReader.cxx',