[Connection][WiFi] Remove duplicate codes (#385)
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiErrorFactory.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.WiFi
21 {
22     static internal class WiFiErrorValue
23     {
24         internal const int Base = -0x01CE0000;
25     }
26
27     internal enum WiFiError
28     {
29         None = ErrorCode.None,
30         InvalidParameterError = ErrorCode.InvalidParameter,
31         OutOfMemoryError = ErrorCode.OutOfMemory,
32         InvalidOperationError = ErrorCode.InvalidOperation,
33         AddressFamilyNotSupportedError = ErrorCode.AddressFamilyNotSupported,
34         OperationFailedError = WiFiErrorValue.Base | 0x01,
35         NoConnectionError = WiFiErrorValue.Base | 0x02,
36         NowInProgressError = ErrorCode.NowInProgress,
37         AlreadyExistsError = WiFiErrorValue.Base | 0x03,
38         OperationAbortedError = WiFiErrorValue.Base | 0x04,
39         DhcpFailedError = WiFiErrorValue.Base | 0x05,
40         InvalidKeyError = WiFiErrorValue.Base | 0x06,
41         NoReplyError = WiFiErrorValue.Base | 0x07,
42         SecurityRestrictedError = WiFiErrorValue.Base | 0x08,
43         PermissionDeniedError = ErrorCode.PermissionDenied,
44         NotSupportedError = ErrorCode.NotSupported
45     }
46
47     internal static class WiFiErrorFactory
48     {
49         static internal void ThrowWiFiException(int e, IntPtr handle)
50         {
51             ThrowException(e, (handle == IntPtr.Zero), false, "");
52         }
53
54         static internal void ThrowWiFiException(int e, IntPtr handle1, IntPtr handle2)
55         {
56             ThrowException(e, (handle1 == IntPtr.Zero), (handle2 == IntPtr.Zero), "");
57         }
58
59         static internal void ThrowWiFiException(int e, string message)
60         {
61             ThrowException(e, false, false, message);
62         }
63
64         static internal void ThrowWiFiException(int e, IntPtr handle, string message)
65         {
66             ThrowException(e, (handle == IntPtr.Zero), false, message);
67         }
68
69         static internal void ThrowWiFiException(int e, IntPtr handle1, IntPtr handle2, string message)
70         {
71             ThrowException(e, (handle1 == IntPtr.Zero), (handle2 == IntPtr.Zero), message);
72         }
73
74         static private void ThrowException(int e, bool isHandle1Null, bool isHandle2Null, string message)
75         {
76             WiFiError err = (WiFiError)e;
77             if (err == WiFiError.NotSupportedError)
78             {
79                 throw new NotSupportedException("Unsupported feature http://tizen.org/feature/network.wifi");
80             }
81
82             if (err == WiFiError.PermissionDeniedError)
83             {
84                 throw new UnauthorizedAccessException("Permission denied " + message);
85             }
86
87             if (err == WiFiError.OutOfMemoryError)
88             {
89                 throw new OutOfMemoryException("Out of memory");
90             }
91
92             if (err == WiFiError.InvalidParameterError || err == WiFiError.InvalidKeyError)
93             {
94                 if (isHandle1Null || isHandle2Null)
95                 {
96                     throw new InvalidOperationException("Invalid instance (object may have been disposed or released)");
97                 }
98
99                 throw new ArgumentException("Invalid parameter");
100             }
101
102             throw new InvalidOperationException(err.ToString());
103         }
104     }
105 }