Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Messaging / Tizen.Messaging.Messages / MessagesErrorFactory.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.Messaging.Messages
21 {
22     internal enum MessagesError
23     {
24         None = ErrorCode.None,
25         OutOfMemory = ErrorCode.OutOfMemory,
26         InvalidParameter = ErrorCode.InvalidParameter,
27         ServerNotReady = -0x01710000 | 0x501,
28         CommunicationWithServerFailed = -0x01710000 | 0x502,
29         OutOfRange = -0x01710000 | 0x503,
30         SendingFailed = -0x01710000 | 0x504,
31         OperationFailed = -0x01710000 | 0x505,
32         NoSimCard = -0x01710000 | 0x506,
33         NoData = -0x01710000 | 0x507,
34         PermissionDenied = ErrorCode.PermissionDenied,
35         NotSupported = ErrorCode.NotSupported
36     }
37
38     internal static class MessagesErrorFactory
39     {
40         static internal void ThrowMessagesException(int e)
41         {
42             ThrowException(e, false);
43         }
44
45         static internal void ThrowMessagesException(int e, IntPtr handle)
46         {
47             ThrowException(e, (handle == IntPtr.Zero));
48         }
49
50         static private void ThrowException(int e, bool isHandleNull)
51         {
52             MessagesError err = (MessagesError)e;
53
54             if (isHandleNull)
55             {
56                 throw new InvalidOperationException("Invalid instance (object may have been disposed or release)");
57             }
58
59             switch (err)
60             {
61                 case MessagesError.OutOfMemory:
62                     throw new OutOfMemoryException(err.ToString());
63                 case MessagesError.InvalidParameter:
64                     throw new ArgumentException(err.ToString());
65                 case MessagesError.PermissionDenied:
66                     throw new UnauthorizedAccessException(err.ToString());
67                 case MessagesError.NotSupported:
68                     throw new NotSupportedException(err.ToString());
69                 default:
70                     throw new InvalidOperationException(err.ToString());
71             }
72         }
73     }
74 }