2
0

OciErrorHandle.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // OciErrorHandle.cs
  3. //
  4. // Part of managed C#/.NET library System.Data.OracleClient.dll
  5. //
  6. // Part of the Mono class libraries at
  7. // mcs/class/System.Data.OracleClient/System.Data.OracleClient.Oci
  8. //
  9. // Assembly: System.Data.OracleClient.dll
  10. // Namespace: System.Data.OracleClient.Oci
  11. //
  12. // Author:
  13. // Tim Coleman <[email protected]>
  14. //
  15. // Copyright (C) Tim Coleman, 2003
  16. //
  17. using System;
  18. using System.Runtime.InteropServices;
  19. using System.Text;
  20. namespace System.Data.OracleClient.Oci {
  21. internal sealed class OciErrorHandle : OciHandle, IDisposable
  22. {
  23. #region Fields
  24. bool disposed = false;
  25. #endregion // Fields
  26. #region Constructors
  27. public OciErrorHandle (OciHandle parent, IntPtr newHandle)
  28. : base (OciHandleType.Error, parent, newHandle)
  29. {
  30. }
  31. #endregion // Constructors
  32. #region Methods
  33. protected override void Dispose (bool disposing)
  34. {
  35. if (!disposed) {
  36. disposed = true;
  37. base.Dispose (disposing);
  38. }
  39. }
  40. public static void ThrowExceptionIfError (OciHandle hwnd, int status)
  41. {
  42. if (status == 0)
  43. return;
  44. OciErrorInfo info = HandleError (hwnd, status);
  45. throw new OracleException (info.ErrorCode, info.ErrorMessage);
  46. }
  47. public static OciErrorInfo HandleError (OciHandle hwnd, int status)
  48. {
  49. OciErrorInfo info;
  50. info.ErrorCode = status;
  51. info.ErrorMessage = OciGlue.ReturnCodeToString (status);
  52. if (status == OciGlue.OCI_ERROR || status == OciGlue.OCI_SUCCESS_WITH_INFO) {
  53. OciHandle h = hwnd;
  54. if (h == null)
  55. throw new Exception ("Internal driver error: handle is null.");
  56. int errbufSize = 4096;
  57. IntPtr errbuf = OciCalls.AllocateClear (errbufSize);
  58. OciCalls.OCIErrorGet (hwnd,
  59. 1,
  60. IntPtr.Zero,
  61. out info.ErrorCode,
  62. errbuf,
  63. (uint) errbufSize,
  64. OciHandleType.Error);
  65. byte[] bytea = new byte[errbufSize];
  66. Marshal.Copy (errbuf, bytea, 0, errbufSize);
  67. errbufSize = 0;
  68. // first call to OCICharSetToUnicode gets the size
  69. OciCalls.OCICharSetToUnicode (h, null, bytea, out errbufSize);
  70. StringBuilder str = new StringBuilder (errbufSize);
  71. // second call to OCICharSetToUnicode gets the string
  72. OciCalls.OCICharSetToUnicode (h, str, bytea, out errbufSize);
  73. string errmsg = String.Empty;
  74. if (errbufSize > 0) {
  75. errmsg = str.ToString ();
  76. info.ErrorMessage = String.Copy (errmsg);
  77. }
  78. Marshal.FreeHGlobal (errbuf);
  79. }
  80. return info;
  81. }
  82. public static OciErrorInfo HandleError (OciHandle hand)
  83. {
  84. OciErrorInfo info;
  85. info.ErrorCode = 0;
  86. info.ErrorMessage = String.Empty;
  87. int errbufSize = 4096;
  88. IntPtr errbuf = OciCalls.AllocateClear (errbufSize);
  89. OciCalls.OCIErrorGet (hand,
  90. 1,
  91. IntPtr.Zero,
  92. out info.ErrorCode,
  93. errbuf,
  94. (uint) errbufSize,
  95. OciHandleType.Error);
  96. byte[] bytea = new byte[errbufSize];
  97. Marshal.Copy (errbuf, bytea, 0, errbufSize);
  98. errbufSize = 0;
  99. OciHandle h = hand.Parent;
  100. if (h == null)
  101. h = hand;
  102. if (h == null)
  103. throw new Exception ("Internal driver error: handle is null.");
  104. // first call to OCICharSetToUnicode gets the size
  105. OciCalls.OCICharSetToUnicode (h, null, bytea, out errbufSize);
  106. StringBuilder str = new StringBuilder (errbufSize);
  107. // second call to OCICharSetToUnicode gets the string
  108. OciCalls.OCICharSetToUnicode (h, str, bytea, out errbufSize);
  109. string errmsg = String.Empty;
  110. if (errbufSize > 0)
  111. errmsg = str.ToString ();
  112. else
  113. errmsg = "Internal driver error. Could not retrieve error message.";
  114. info.ErrorMessage = String.Copy (errmsg);
  115. Marshal.FreeHGlobal (errbuf);
  116. return info;
  117. }
  118. public OciErrorInfo HandleError ()
  119. {
  120. return HandleError (this);
  121. }
  122. #endregion // Methods
  123. }
  124. }