]>
git.proxmox.com Git - cargo.git/blob - vendor/openssl-0.9.23/src/error.rs
1 //! Errors returned by OpenSSL library.
3 //! OpenSSL errors are stored in an `ErrorStack`. Most methods in the crate
4 //! returns a `Result<T, ErrorStack>` type.
9 //! use openssl::error::ErrorStack;
10 //! use openssl::bn::BigNum;
12 //! let an_error = BigNum::from_dec_str("Cannot parse letters");
15 //! Err(e) => println!("Parsing Error: {:?}", e),
18 use libc
::{c_ulong, c_char, c_int}
;
29 /// Collection of [`Error`]s from OpenSSL.
31 /// [`Error`]: struct.Error.html
32 #[derive(Debug, Clone)]
33 pub struct ErrorStack(Vec
<Error
>);
36 /// Returns the contents of the OpenSSL error stack.
37 pub fn get() -> ErrorStack
{
39 while let Some(err
) = Error
::get() {
47 /// Returns the errors in the stack.
48 pub fn errors(&self) -> &[Error
] {
53 impl fmt
::Display
for ErrorStack
{
54 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
60 write
!(fmt
, "{}", err
)?
;
67 impl error
::Error
for ErrorStack
{
68 fn description(&self) -> &str {
69 "An OpenSSL error stack"
73 impl From
<ErrorStack
> for io
::Error
{
74 fn from(e
: ErrorStack
) -> io
::Error
{
75 io
::Error
::new(io
::ErrorKind
::Other
, e
)
79 impl From
<ErrorStack
> for fmt
::Error
{
80 fn from(_
: ErrorStack
) -> fmt
::Error
{
85 /// An error reported from OpenSSL.
91 data
: Option
<Cow
<'
static, str>>,
94 unsafe impl Sync
for Error {}
95 unsafe impl Send
for Error {}
98 /// Returns the first error on the OpenSSL error stack.
99 pub fn get() -> Option
<Error
> {
103 let mut file
= ptr
::null();
105 let mut data
= ptr
::null();
107 match ffi
::ERR_get_error_line_data(&mut file
, &mut line
, &mut data
, &mut flags
) {
110 // The memory referenced by data is only valid until that slot is overwritten
111 // in the error stack, so we'll need to copy it off if it's dynamic
112 let data
= if flags
& ffi
::ERR_TXT_STRING
!= 0 {
113 let bytes
= CStr
::from_ptr(data
as *const _
).to_bytes();
114 let data
= str::from_utf8(bytes
).unwrap();
115 let data
= if flags
& ffi
::ERR_TXT_MALLOCED
!= 0 {
116 Cow
::Owned(data
.to_string())
135 /// Returns the raw OpenSSL error code for this error.
136 pub fn code(&self) -> c_ulong
{
140 /// Returns the name of the library reporting the error, if available.
141 pub fn library(&self) -> Option
<&'
static str> {
143 let cstr
= ffi
::ERR_lib_error_string(self.code
);
147 let bytes
= CStr
::from_ptr(cstr
as *const _
).to_bytes();
148 Some(str::from_utf8(bytes
).unwrap())
152 /// Returns the name of the function reporting the error.
153 pub fn function(&self) -> Option
<&'
static str> {
155 let cstr
= ffi
::ERR_func_error_string(self.code
);
159 let bytes
= CStr
::from_ptr(cstr
as *const _
).to_bytes();
160 Some(str::from_utf8(bytes
).unwrap())
164 /// Returns the reason for the error.
165 pub fn reason(&self) -> Option
<&'
static str> {
167 let cstr
= ffi
::ERR_reason_error_string(self.code
);
171 let bytes
= CStr
::from_ptr(cstr
as *const _
).to_bytes();
172 Some(str::from_utf8(bytes
).unwrap())
176 /// Returns the name of the source file which encountered the error.
177 pub fn file(&self) -> &'
static str {
179 assert
!(!self.file
.is_null());
180 let bytes
= CStr
::from_ptr(self.file
as *const _
).to_bytes();
181 str::from_utf8(bytes
).unwrap()
185 /// Returns the line in the source file which encountered the error.
186 pub fn line(&self) -> c_int
{
190 /// Returns additional data describing the error.
191 pub fn data(&self) -> Option
<&str> {
192 self.data
.as_ref().map(|s
| &**s
)
196 impl fmt
::Debug
for Error
{
197 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
198 let mut builder
= fmt
.debug_struct("Error");
199 builder
.field("code", &self.code());
200 if let Some(library
) = self.library() {
201 builder
.field("library", &library
);
203 if let Some(function
) = self.function() {
204 builder
.field("function", &function
);
206 if let Some(reason
) = self.reason() {
207 builder
.field("reason", &reason
);
209 builder
.field("file", &self.file());
210 builder
.field("line", &self.line());
211 if let Some(data
) = self.data() {
212 builder
.field("data", &data
);
218 impl fmt
::Display
for Error
{
219 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
220 write
!(fmt
, "error:{:08X}", self.code())?
;
221 match self.library() {
222 Some(l
) => write
!(fmt
, ":{}", l
)?
,
223 None
=> write
!(fmt
, ":lib({})", ffi
::ERR_GET_LIB(self.code()))?
,
225 match self.function() {
226 Some(f
) => write
!(fmt
, ":{}", f
)?
,
227 None
=> write
!(fmt
, ":func({})", ffi
::ERR_GET_FUNC(self.code()))?
,
229 match self.reason() {
230 Some(r
) => write
!(fmt
, ":{}", r
)?
,
231 None
=> write
!(fmt
, ":reason({})", ffi
::ERR_GET_FUNC(self.code()))?
,
238 self.data().unwrap_or("")
243 impl error
::Error
for Error
{
244 fn description(&self) -> &str {