[Interop] merge similar branch in switch
[platform/core/csapi/tizenfx.git] / src / Tizen.Multimedia / Interop / Interop.ErrorCode.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18 using System.IO;
19 using System.Runtime.CompilerServices;
20 using Tizen;
21
22 namespace Tizen.Multimedia
23 {
24     internal static partial class Interop
25     {
26         internal enum ErrorCode
27         {
28             None = Tizen.Internals.Errors.ErrorCode.None,
29             OutOfMemory = Tizen.Internals.Errors.ErrorCode.OutOfMemory,
30             InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter,
31             InvalidOperation = Tizen.Internals.Errors.ErrorCode.InvalidOperation,
32             PermissionDenied = Tizen.Internals.Errors.ErrorCode.PermissionDenied,
33             NotSupported = Tizen.Internals.Errors.ErrorCode.NotSupported,
34             ResourceBusy = Tizen.Internals.Errors.ErrorCode.ResourceBusy,
35             NoSuchFile = Tizen.Internals.Errors.ErrorCode.NoSuchFile,
36
37             // Radio
38             InvalidState = -0x019A0000 | 0x01, // RADIO_ERROR_INVALID_STATE
39             SoundPolicy = -0x019A0000 | 0x02, // RADIO_ERROR_SOUND_POLICY
40             NoAntenna = -0x019A0000 | 0x03, // RADIO_ERROR_NO_ANTENNA
41
42             // Image/ Video Utility
43             NotSupportedFormat = -0x01980000 | 0x01, // VIDEO_UTIL_ERROR_NOT_SUPPORTED_FORMAT
44         }
45     }
46
47     internal static class ErrorCodeExtensions
48     {
49         private const string LogTag = "Tizen.Multimedia";
50
51         internal static bool IsSuccess(this Interop.ErrorCode err)
52         {
53             return err == Interop.ErrorCode.None;
54         }
55
56         internal static bool IsFailed(this Interop.ErrorCode err)
57         {
58             return !err.IsSuccess();
59         }
60
61         /// <summary>
62         /// Utility method to check for error, returns false if failed and print warning messages
63         /// </summary>
64         /// <returns>true in case of no error, false otherwise</returns>
65         internal static bool WarnIfFailed(this Interop.ErrorCode err, string msg, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
66         {
67             if (err.IsFailed())
68             {
69                 Log.Debug(LogTag, $"{msg}, err: {err.ToString()}", file, func, line);
70                 return false;
71             }
72             return true;
73         }
74
75         /// <summary>
76         /// Utility method to check for error, returns false if failed and throw exception
77         /// </summary>
78         /// <returns>true in case of no error</returns>
79         internal static bool ThrowIfFailed(this Interop.ErrorCode err, string msg, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
80         {
81             if (err.IsFailed())
82             {
83                 Log.Error(LogTag, $"{msg}, err: {err.ToString()}", file, func, line);
84                 throw err.GetException(msg);
85             }
86             return true;
87         }
88
89         internal static Exception GetException(this Interop.ErrorCode err, string message)
90         {
91             string errMessage = $"{message}, err: {err.ToString()}";
92             switch (err)
93             {
94                 //case ErrorCode.None:
95                 case Interop.ErrorCode.PermissionDenied: return new UnauthorizedAccessException(errMessage);
96                 case Interop.ErrorCode.InvalidParameter: return new ArgumentException(errMessage);
97                 case Interop.ErrorCode.NoSuchFile: return new FileNotFoundException(errMessage);
98                 case Interop.ErrorCode.OutOfMemory: return new OutOfMemoryException(errMessage);
99                 case Interop.ErrorCode.NoAntenna:
100                 case Interop.ErrorCode.NotSupported: return new NotSupportedException(errMessage);
101                 case Interop.ErrorCode.InvalidOperation:
102                 case Interop.ErrorCode.InvalidState:
103                 case Interop.ErrorCode.SoundPolicy:
104                 case Interop.ErrorCode.ResourceBusy:
105                 default: return new InvalidOperationException(errMessage);
106             }
107         }
108     }
109 }