~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/ServiceStack/src/ServiceStack.Common/ServiceClient.Web/WebServiceException.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.Collections.Generic;
 
3
using ServiceStack.Common.Utils;
 
4
using ServiceStack.ServiceInterface.ServiceModel;
 
5
using ServiceStack.Text;
 
6
 
 
7
namespace ServiceStack.ServiceClient.Web
 
8
{
 
9
    public class WebServiceException
 
10
        : Exception
 
11
    {
 
12
        public WebServiceException() { }
 
13
        public WebServiceException(string message) : base(message) { }
 
14
        public WebServiceException(string message, Exception innerException) : base(message, innerException) { }
 
15
 
 
16
        public int StatusCode { get; set; }
 
17
 
 
18
        public string StatusDescription { get; set; }
 
19
 
 
20
        public object ResponseDto { get; set; }
 
21
        
 
22
        public string ResponseBody { get; set; }
 
23
 
 
24
        private string errorCode;
 
25
 
 
26
        private void ParseResponseDto()
 
27
        {
 
28
            if (ResponseDto == null)
 
29
            {
 
30
                errorCode = StatusDescription;
 
31
                return;
 
32
            }
 
33
            var jsv = TypeSerializer.SerializeToString(ResponseDto);
 
34
            var map = TypeSerializer.DeserializeFromString<Dictionary<string, string>>(jsv);
 
35
            map = new Dictionary<string, string>(map, StringComparer.InvariantCultureIgnoreCase);
 
36
            string responseStatus;
 
37
            if (!map.TryGetValue("ResponseStatus", out responseStatus)) return;
 
38
 
 
39
            var rsMap = TypeSerializer.DeserializeFromString<Dictionary<string, string>>(responseStatus);
 
40
            if (rsMap == null) return;
 
41
            rsMap = new Dictionary<string, string>(rsMap, StringComparer.InvariantCultureIgnoreCase);
 
42
            rsMap.TryGetValue("ErrorCode", out errorCode);
 
43
            rsMap.TryGetValue("Message", out errorMessage);
 
44
            rsMap.TryGetValue("StackTrace", out serverStackTrace);
 
45
        }
 
46
 
 
47
        public string ErrorCode
 
48
        {
 
49
            get
 
50
            {
 
51
                if (errorCode == null)
 
52
                {
 
53
                    ParseResponseDto();
 
54
                }
 
55
                return errorCode;
 
56
            }
 
57
        }
 
58
 
 
59
        private string errorMessage;
 
60
        public string ErrorMessage
 
61
        {
 
62
            get
 
63
            {
 
64
                if (errorMessage == null)
 
65
                {
 
66
                    ParseResponseDto();
 
67
                }
 
68
                return errorMessage;
 
69
            }
 
70
        }
 
71
 
 
72
        private string serverStackTrace;
 
73
        public string ServerStackTrace
 
74
        {
 
75
            get
 
76
            {
 
77
                if (serverStackTrace == null)
 
78
                {
 
79
                    ParseResponseDto();
 
80
                }
 
81
                return serverStackTrace;
 
82
            }
 
83
        }
 
84
 
 
85
        public ResponseStatus ResponseStatus
 
86
        {
 
87
            get
 
88
            {
 
89
                if (this.ResponseDto == null)
 
90
                    return null;
 
91
 
 
92
                var hasResponseStatus = this.ResponseDto as IHasResponseStatus;
 
93
                if (hasResponseStatus != null)
 
94
                    return hasResponseStatus.ResponseStatus;
 
95
 
 
96
                var propertyInfo = this.ResponseDto.GetType().GetProperty("ResponseStatus");
 
97
                if (propertyInfo == null)
 
98
                    return null;
 
99
 
 
100
                return ReflectionUtils.GetProperty(this.ResponseDto, propertyInfo) as ResponseStatus;
 
101
            }
 
102
        }
 
103
 
 
104
        public List<ResponseError> GetFieldErrors()
 
105
        {
 
106
            var responseStatus = ResponseStatus;
 
107
            if (responseStatus != null)
 
108
                return responseStatus.Errors ?? new List<ResponseError>();
 
109
 
 
110
            return new List<ResponseError>();
 
111
        }
 
112
    }
 
113
}