fork/rust-sdks
git clone https://git.julia.blue/fork/rust-sdks
55bda13
livekit/livekit-ffi@0.13.0
1/* 2* Copyright 2023 LiveKit 3* 4* Licensed under the Apache License, Version 2.0 (the “License”); 5* you may not use this file except in compliance with the License. 6* You may obtain a copy of the License at 7* 8* http://www.apache.org/licenses/LICENSE-2.0 9* 10* Unless required by applicable law or agreed to in writing, software 11* distributed under the License is distributed on an “AS IS” BASIS, 12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13* See the License for the specific language governing permissions and 14* limitations under the License. 15*/ 16 17#include "livekit/rtc_error.h" 18 19#include <iomanip> 20#include <sstream> 21#include <string> 22 23namespace livekit { 24 25RtcError to_error (const webrtc::RTCError & error ) { 26RtcError lk_error ; 27lk_error .error_detail = static_cast < RtcErrorDetailType > (error .error_detail ()); 28lk_error .error_type = static_cast < RtcErrorType > (error .type ()); 29lk_error .has_sctp_cause_code = error .sctp_cause_code ().has_value (); 30lk_error .sctp_cause_code = error .sctp_cause_code ().value_or (0 ); 31lk_error .message = error .message (); 32return lk_error ; 33} 34 35std::string serialize_error (const RtcError & error ) { 36 std::stringstream ss ; 37ss << std::hex << std::setfill ('0' ); 38ss << std::setw (8 ) << (uint32_t )error .error_type ; 39ss << std::setw (8 ) << (uint32_t )error .error_detail ; 40ss << std::setw (2 ) << (uint16_t )error .has_sctp_cause_code ; 41ss << std::setw (4 ) << (uint16_t )error .sctp_cause_code ; 42ss << std::dec << std::setw (1 ) << std::string (error .message ); 43return ss .str (); 44} 45 46#ifdef LIVEKIT_TEST 47rust::String serialize_deserialize () { 48RtcError lk_error ; 49lk_error .error_type = RtcErrorType ::InternalError ; 50lk_error .error_detail = RtcErrorDetailType ::DataChannelFailure ; 51lk_error .has_sctp_cause_code = true; 52lk_error .sctp_cause_code = 24 ; 53lk_error .message = "this is not a test, I repeat, this is not a test" ; 54return serialize_error (lk_error ); 55} 56 57void throw_error () { 58RtcError lk_error ; 59lk_error .error_type = RtcErrorType ::InvalidModification ; 60lk_error .error_detail = RtcErrorDetailType ::None ; 61lk_error .has_sctp_cause_code = false; 62lk_error .sctp_cause_code = 0 ; 63lk_error .message = "exception is thrown!" ; 64throw std::runtime_error (serialize_error (lk_error )); 65} 66#endif 67 68}// namespace livekit