Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Mtp / Tizen.Network.Mtp / MtpErrorFactory.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 Tizen.Internals.Errors;
19
20 namespace Tizen.Network.Mtp
21 {
22     static internal class MtpErrorValue
23     {
24         internal const int Base = -0x01CC0000;
25     }
26
27     /// <summary>
28     /// Enumeration for Mtp Error.
29     /// </summary>
30     /// <since_tizen> 5 </since_tizen>
31     public enum MtpError
32     {
33         None = ErrorCode.None,
34         IoError = ErrorCode.IoError,
35         InvalidParameterError = ErrorCode.InvalidParameter,
36         OutOfMemoryError = ErrorCode.OutOfMemory,
37         PermissionDeniedError = ErrorCode.PermissionDenied,
38         NotSupportedError = ErrorCode.NotSupported,
39
40         CommunicationError = MtpErrorValue.Base | 0x01,
41         ControllerError = MtpErrorValue.Base | 0x02,
42         NoDeviceError = MtpErrorValue.Base | 0x03,
43         NotInitializedError = MtpErrorValue.Base | 0x04,
44         NotActivatedError = MtpErrorValue.Base | 0x05,
45         NotActivatedCommunicationError = MtpErrorValue.Base | 0x06,
46         PluginFailError = MtpErrorValue.Base | 0x07
47     }
48
49     internal static class MtpErrorFactory
50     {
51         static internal void ThrowMtpException(int e)
52         {
53             ThrowException(e, false);
54         }
55
56         static internal void ThrowMtpException(int e, int handle)
57         {
58             ThrowException(e, (handle < 0));
59         }
60
61         static private void ThrowException(int e, bool isHandleNull)
62         {
63             MtpError err = (MtpError)e;
64
65             if (isHandleNull)
66             {
67                 throw new InvalidOperationException("Invalid instance (object may have been disposed or released)");
68             }
69
70             if (err == MtpError.InvalidParameterError)
71             {
72                 throw new ArgumentException(err.ToString());
73             }
74             else if (err == MtpError.NotSupportedError)
75             {
76                 throw new NotSupportedException();
77             }
78             else
79             {
80                 throw new InvalidOperationException(err.ToString());
81             }
82         }
83
84     }
85 }