summaryrefslogtreecommitdiff
path: root/cs/src/Ice/Exception.cs
blob: f8283c29dece07d5c7f7b4bd0af4681da0dc0069 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

using System.Diagnostics;

namespace IceInternal
{
    public class Ex
    {
        public static void throwUOE(string expectedType, string actualType)
        {
            throw new Ice.UnexpectedObjectException(
                        "expected element of type `" + expectedType + "' but received '" + actualType,
                        actualType, expectedType);
        }
    }
}

namespace Ice
{

    public abstract class Exception : System.Exception, System.ICloneable
    {
        public object Clone()
        {
            return MemberwiseClone();
        }

        public Exception() {}
        public Exception(System.Exception ex) : base("", ex) {}
        public abstract string ice_name();
        public override string ToString()
        {
            //
            // This prints the exception Java style. That is, the outermost
            // exception, "Caused by:" to the innermost exception. The
            // stack trace is not nicely indented as with Java, but
            // without string parsing (perhaps tokenize on "\n"), it
            // doesn't appear to be possible to reformat it.
            //
            System.IO.StringWriter sw = new System.IO.StringWriter();
            IceUtilInternal.OutputBase op = new IceUtilInternal.OutputBase(sw);
            op.setUseTab(false);
            op.print(GetType().FullName);
            op.inc();
            IceInternal.ValueWriter.write(this, op);
            sw.Write("\n");
            sw.Write(StackTrace);

            System.Exception curr = InnerException;
            while(curr != null)
            {
                sw.Write("\nCaused by: ");
                sw.Write(curr.GetType().FullName);
                if(!(curr is Ice.Exception))
                {
                    sw.Write(": ");
                    sw.Write(curr.Message);
                }
                sw.Write("\n");
                sw.Write(curr.StackTrace);
                curr = curr.InnerException;
            }

            return sw.ToString();
        }
    }

    public abstract class LocalException : Exception
    {
        public LocalException() {}
        public LocalException(System.Exception ex) : base(ex) {}
    }

    public abstract class SystemException : Exception
    {
        public SystemException() {}
        public SystemException(System.Exception ex) : base(ex) {}
    }

    public abstract class UserException : Exception
    {
        public UserException() {}
        public UserException(System.Exception ex) : base(ex) {}
        public abstract void write__(IceInternal.BasicStream os__);
        public abstract void read__(IceInternal.BasicStream is__, bool rid__);

        public virtual void write__(Ice.OutputStream outS__)
        {
            Debug.Assert(false);
        }

        public virtual void read__(Ice.InputStream inS__, bool rid__)
        {
            Debug.Assert(false);
        }

        public virtual bool usesClasses__()
        {
            return false;
        }
    }

}

namespace IceInternal
{

    public class LocalExceptionWrapper : Ice.Exception
    {
        public LocalExceptionWrapper(Ice.LocalException ex, bool retry)
        {
            _ex = ex;
            _retry = retry;
        }

        public LocalExceptionWrapper(LocalExceptionWrapper ex)
        {
            _ex = ex.get();
            _retry = ex._retry;
        }

        public override string ice_name()
        {
            return _ex.ice_name();
        }

        public Ice.LocalException get()
        {
            return _ex;
        }

        //
        // If true, always repeat the request. Don't take retry settings
        // or "at-most-once" guarantees into account.
        //
        // If false, only repeat the request if the retry settings allow
        // to do so, and if "at-most-once" does not need to be guaranteed.
        //
        public bool retry()
        {
            return _retry;
        }

        public static void throwWrapper(System.Exception ex)
        {
            if(ex is Ice.UserException)
            {
                throw new LocalExceptionWrapper(new Ice.UnknownUserException(((Ice.UserException)ex).ice_name()), false);
            }

            if(ex is Ice.LocalException)
            {
                if(ex is Ice.UnknownException ||
                   ex is Ice.ObjectNotExistException ||
                   ex is Ice.OperationNotExistException ||
                   ex is Ice.FacetNotExistException)
                {
                    throw new LocalExceptionWrapper((Ice.LocalException)ex, false);
                }
                throw new LocalExceptionWrapper(new Ice.UnknownLocalException(((Ice.LocalException)ex).ice_name()), false);
            }
            throw new LocalExceptionWrapper(new Ice.UnknownException(ex.GetType().FullName), false);
        }
        
        private Ice.LocalException _ex;
        private bool _retry;
    }

}