about summary refs log blame commit diff stats
path: root/crates/libmpv2/src/mpv/errors.rs
blob: a2baee5a586ddb3d798f2567232210cc46bd00ff (plain) (tree)












































































































                                                                                                                                                                                                                                                                                                                                                                                                          
// yt - A fully featured command line YouTube client
//
// Copyright (C) 2024 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This file is part of Yt.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>.

use std::{ffi::NulError, os::raw as ctype, str::Utf8Error};

use thiserror::Error;

use super::mpv_error;

#[allow(missing_docs)]
pub type Result<T> = ::std::result::Result<T, Error>;

#[derive(Error, Debug)]
pub enum Error {
    #[error("loading file failed: {error}")]
    Loadfile { error: String },

    #[error("version mismatch detected! Linked version ({linked}) is unequal to the loaded version ({loaded})")]
    VersionMismatch {
        linked: ctype::c_ulong,
        loaded: ctype::c_ulong,
    },

    #[error("invalid utf8 returned")]
    InvalidUtf8,

    #[error("null pointer returned")]
    Null,

    #[error("raw error returned: {}", to_string_mpv_error(*(.0)))]
    Raw(crate::MpvError),
}

impl From<NulError> for Error {
    fn from(_other: NulError) -> Error {
        Error::Null
    }
}

impl From<Utf8Error> for Error {
    fn from(_other: Utf8Error) -> Error {
        Error::InvalidUtf8
    }
}
impl From<crate::MpvError> for Error {
    fn from(other: crate::MpvError) -> Error {
        Error::Raw(other)
    }
}

pub(crate) fn to_string_mpv_error(num: crate::MpvError) -> String {
    let (error, help) = to_string_mpv_error_raw(num);

    if help.is_empty() {
        error.to_owned()
    } else {
        format!("{} ({})", error, help)
    }
}

fn to_string_mpv_error_raw(num: crate::MpvError) -> (&'static str, &'static str) {
    // debug!("Turning error num '{}' to a string.", num);

    match num {
        mpv_error::EventQueueFull => (
            "The event ringbuffer is full.",
            "This means the client is choked, and can't receive any events. This can happen when too many asynchronous requests have been made, but not answered. Probably never happens in practice, unless the mpv core is frozen for some reason, and the client keeps making asynchronous requests. (Bugs in the client API implementation could also trigger this, e.g. if events become \"lost\".)",
        ),

        mpv_error::NoMem => ("Memory allocation failed.", ""),

        mpv_error::Uninitialized => ("The mpv core wasn't configured and initialized yet", " See the notes in mpv_create()."),

        mpv_error::InvalidParameter => ("Generic catch-all error if a parameter is set to an invalid or unsupported value.", "This is used if there is no better error code."),

        mpv_error::OptionNotFound => ("Trying to set an option that doesn't exist.", ""),
        mpv_error::OptionFormat => ("Trying to set an option using an unsupported MPV_FORMAT.", ""),
        mpv_error::OptionError => ("Setting the option failed", " Typically this happens if the provided option value could not be parsed."),

        mpv_error::PropertyNotFound => ("The accessed property doesn't exist.", ""),
        mpv_error::PropertyFormat => ("Trying to set or get a property using an unsupported MPV_FORMAT.", ""),
        mpv_error::PropertyUnavailable => ("The property exists, but is not available", "This usually happens when the associated subsystem is not active, e.g. querying audio parameters while audio is disabled."),
        mpv_error::PropertyError => ("Error setting or getting a property.", ""),

        mpv_error::Command => ("General error when running a command with mpv_command and similar.", ""),

        mpv_error::LoadingFailed => ("Generic error on loading (usually used with mpv_event_end_file.error).", ""),

        mpv_error::AoInitFailed => ("Initializing the audio output failed.", ""),
        mpv_error::VoInitFailed => ("Initializing the video output failed.", ""),

        mpv_error::NothingToPlay => ("There was no audio or video data to play", "This also happens if the file was recognized, but did not contain any audio or video streams, or no streams were selected."),

        mpv_error::UnknownFormat => ("     * When trying to load the file, the file format could not be determined, or the file was too broken to open it.", ""),

        mpv_error::Generic => ("Generic error for signaling that certain system requirements are not fulfilled.", ""),
        mpv_error::NotImplemented => ("The API function which was called is a stub only", ""),
        mpv_error::Unsupported => ("Unspecified error.", ""),

        mpv_error::Success => unreachable!("This is not an error. It's just here, to ensure that the 0 case marks an success'"),
        _ => unreachable!("Mpv seems to have changed it's constants."),
    }
}