forked from ravendb/ravendb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ErrorPresenter.cs
118 lines (102 loc) · 2.96 KB
/
ErrorPresenter.cs
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
// -----------------------------------------------------------------------
// <copyright file="ErrorPresenter.cs" company="Hibernating Rhinos LTD">
// Copyright (c) Hibernating Rhinos LTD. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using Raven.Json.Linq;
using Raven.Studio.Features.Util;
namespace Raven.Studio.Infrastructure
{
public static class ErrorPresenter
{
private static bool isErrorWindowVisible;
public static void Show(Exception e, StackTrace innerStackTrace = null, string customMessage = null)
{
var writer = new StringWriter();
if (customMessage != null)
{
WriteCustomMessage(customMessage, writer);
}
else
{
SetCustomMessageBasedOnTheActualError(writer, e);
}
writer.Write("Message: ");
writer.WriteLine(e.Message);
if (string.IsNullOrWhiteSpace(UrlUtil.Url) == false)
{
writer.Write("Uri: ");
writer.WriteLine(UrlUtil.Url);
}
writer.Write("Server Uri: ");
writer.WriteLine(GetServerUri(e));
writer.WriteLine();
writer.WriteLine("-- Error Information --");
writer.WriteLine(e.ToString());
writer.WriteLine();
if (innerStackTrace != null)
{
writer.WriteLine("Inner StackTrace: ");
writer.WriteLine(innerStackTrace.ToString());
}
Show(writer.ToString());
}
private static void WriteCustomMessage(string customMessage, StringWriter writer)
{
writer.WriteLine(customMessage);
writer.WriteLine();
writer.WriteLine();
}
private static void SetCustomMessageBasedOnTheActualError(StringWriter writer, Exception e)
{
var webException = e as WebException;
if (webException != null)
{
var webResponse = webException.Response as HttpWebResponse;
if (webResponse != null)
{
switch (webResponse.StatusCode)
{
case HttpStatusCode.Unauthorized:
case HttpStatusCode.Forbidden:
WriteCustomMessage("Could not authenticate against the server", writer);
break;
default:
using (var streamReader = new StreamReader(webResponse.GetResponseStream()))
{
var error = streamReader.ReadToEnd();
WriteCustomMessage(error, writer);
}
break;
}
}
}
}
public static void Show(string text)
{
if (isErrorWindowVisible)
return;
isErrorWindowVisible = true;
var window = new ErrorWindow(text);
window.Closed += (sender, args) => isErrorWindowVisible = false;
window.Show();
}
private static string GetServerUri(Exception e)
{
if (e.Data.Contains("Url"))
{
var serverUri = (Uri) e.Data["Url"];
if(serverUri != null)
return serverUri.ToString();
return "null";
}
if (e.InnerException != null)
return GetServerUri(e.InnerException);
return "unknown";
}
}
}