81 lines
2.7 KiB
Luau
81 lines
2.7 KiB
Luau
--!strict
|
|
-- core/Err: the error values every decoder / encoder returns (`nil, err`; nothing in the library throws on bad data).
|
|
-- An Err is a table { code, message, offset?, format? } that prints as "png: CORRUPT at 0x1234: bad filter type 7".
|
|
-- Never throws: --!native modules may require it.
|
|
local Err = {}
|
|
|
|
-- codes
|
|
Err.TRUNCATED = "TRUNCATED" -- the data ends early
|
|
Err.CORRUPT = "CORRUPT" -- the data is malformed (bad magic inside, bad table, out-of-range field)
|
|
Err.CHECKSUM = "CHECKSUM" -- a CRC / Adler / hash mismatch
|
|
Err.UNSUPPORTED = "UNSUPPORTED" -- valid data using a feature this library does not implement, or no encoder
|
|
Err.UNKNOWN_FORMAT = "UNKNOWN_FORMAT" -- no format recognised the data / name
|
|
Err.LIMIT = "LIMIT" -- over a size limit (opts.maxPixels, opts.maxBytes, ...)
|
|
Err.ARG = "ARG" -- a bad argument (wrong type, out-of-range option)
|
|
Err.IO = "IO" -- reading or writing a file failed, or the host has no file system
|
|
|
|
export type Err = {
|
|
code: string,
|
|
message: string,
|
|
offset: number?,
|
|
format: string?,
|
|
}
|
|
|
|
local Meta = {}
|
|
Meta.__tostring = function(e: Err): string
|
|
local s = if e.format then e.format .. ": " .. e.code else e.code
|
|
if e.offset then
|
|
s ..= string.format(" at 0x%x", e.offset)
|
|
end
|
|
return s .. ": " .. e.message
|
|
end
|
|
|
|
function Err.new(code: string, message: string, offset: number?): Err
|
|
return setmetatable({ code = code, message = message, offset = offset, format = nil :: string? }, Meta) :: any
|
|
end
|
|
|
|
local KIND: { [string]: string } = {}
|
|
for _, c in { "TRUNCATED", "CORRUPT", "CHECKSUM", "UNSUPPORTED", "LIMIT", "ARG", "IO", "UNKNOWN_FORMAT" } do
|
|
KIND[c] = c
|
|
KIND[string.lower(c)] = c
|
|
end
|
|
|
|
function Err.from(message: any, offset: number?, kind: string?): Err
|
|
if Err.is(message) then
|
|
return message
|
|
end
|
|
local msg = if message == nil then "failed" else tostring(message)
|
|
local code: string? = kind and KIND[kind]
|
|
if not code then
|
|
local head, rest = string.match(msg, "^(%a+):%s*(.*)$")
|
|
if head and KIND[head] then
|
|
code, msg = KIND[head], rest :: string
|
|
elseif string.find(msg, "truncated", 1, true) or string.find(msg, "past the end", 1, true) or string.find(msg, "runs past", 1, true) then
|
|
code = Err.TRUNCATED
|
|
elseif string.find(msg, "^unsupported") then
|
|
code = Err.UNSUPPORTED
|
|
elseif string.find(msg, "^checksum") then
|
|
code = Err.CHECKSUM
|
|
else
|
|
code = Err.CORRUPT
|
|
end
|
|
end
|
|
return Err.new(code :: string, msg, offset)
|
|
end
|
|
|
|
function Err.is(v: any): boolean
|
|
return type(v) == "table" and getmetatable(v) == Meta
|
|
end
|
|
|
|
function Err.wrap(e: any, format: string?): Err
|
|
if not Err.is(e) then
|
|
e = Err.new(Err.CORRUPT, tostring(e))
|
|
end
|
|
if e.format == nil then
|
|
e.format = format
|
|
end
|
|
return e
|
|
end
|
|
|
|
return table.freeze(Err)
|