macro err_check(expr)
    quote
        ret = $expr
        ret == 0 || mbed_err(ret)
        ret
    end
end

immutable MbedException <: Exception
    ret::Cint
end

function show(io::IO, err::MbedException)
    print(io, "MbedTLS error code $(err.ret): $(strerror(err.ret))")
end

mbed_err(ret) = throw(MbedException(ret))

function strerror(ret, bufsize=1000)
    buf = Vector{UInt8}(bufsize)
    ccall((:mbedtls_strerror, MBED_CRYPTO), Cint,
        (Cint, Ptr{Void}, Csize_t),
        ret, buf, bufsize)
    String(pointer(buf))
end
