1+ // Copyright (c) .NET Foundation. All rights reserved.
2+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+ using System ;
5+ using System . Net ;
6+ using System . Net . Http ;
7+ using System . Web ;
8+ using System . Web . Http ;
9+ using System . Web . Mvc ;
10+
11+ namespace NuGetGallery
12+ {
13+ public static class SimulatedErrorTypeExtensions
14+ {
15+ public static HttpResponseMessage MapToWebApiResult ( this SimulatedErrorType type )
16+ {
17+ var message = type . GetMessage ( ) ;
18+ switch ( type )
19+ {
20+ case SimulatedErrorType . Result400 :
21+ return new HttpResponseMessage ( HttpStatusCode . BadRequest )
22+ {
23+ ReasonPhrase = message ,
24+ } ;
25+ case SimulatedErrorType . Result404 :
26+ return new HttpResponseMessage ( HttpStatusCode . NotFound )
27+ {
28+ ReasonPhrase = message ,
29+ } ;
30+ case SimulatedErrorType . Result500 :
31+ return new HttpResponseMessage ( HttpStatusCode . InternalServerError )
32+ {
33+ ReasonPhrase = message ,
34+ } ;
35+ case SimulatedErrorType . Result503 :
36+ return new HttpResponseMessage ( HttpStatusCode . ServiceUnavailable )
37+ {
38+ ReasonPhrase = message ,
39+ } ;
40+ default :
41+ throw type . MapToException ( ) ;
42+ }
43+ }
44+
45+ public static ActionResult MapToMvcResult ( this SimulatedErrorType type )
46+ {
47+ var message = type . GetMessage ( ) ;
48+ switch ( type )
49+ {
50+ case SimulatedErrorType . Result400 :
51+ return new HttpStatusCodeResult ( HttpStatusCode . BadRequest , message ) ;
52+ case SimulatedErrorType . Result404 :
53+ return new HttpStatusCodeResult ( HttpStatusCode . NotFound , message ) ;
54+ case SimulatedErrorType . Result500 :
55+ return new HttpStatusCodeResult ( HttpStatusCode . InternalServerError , message ) ;
56+ case SimulatedErrorType . Result503 :
57+ return new HttpStatusCodeResult ( HttpStatusCode . ServiceUnavailable , message ) ;
58+ default :
59+ throw type . MapToException ( ) ;
60+ }
61+ }
62+ public static string GetMessage ( this SimulatedErrorType type )
63+ {
64+ return $ "{ nameof ( SimulatedErrorType ) } { type } ";
65+ }
66+
67+ public static Exception MapToException ( this SimulatedErrorType type )
68+ {
69+ var message = type . GetMessage ( ) ;
70+ switch ( type )
71+ {
72+ case SimulatedErrorType . HttpException400 :
73+ return new HttpException ( 400 , message ) ;
74+ case SimulatedErrorType . HttpException404 :
75+ return new HttpException ( 404 , message ) ;
76+ case SimulatedErrorType . HttpException500 :
77+ return new HttpException ( 500 , message ) ;
78+ case SimulatedErrorType . HttpException503 :
79+ return new HttpException ( 503 , message ) ;
80+ case SimulatedErrorType . HttpResponseException400 :
81+ return new HttpResponseException ( new HttpResponseMessage ( HttpStatusCode . BadRequest )
82+ {
83+ ReasonPhrase = message ,
84+ } ) ;
85+ case SimulatedErrorType . HttpResponseException404 :
86+ return new HttpResponseException ( new HttpResponseMessage ( HttpStatusCode . NotFound )
87+ {
88+ ReasonPhrase = message ,
89+ } ) ;
90+ case SimulatedErrorType . HttpResponseException500 :
91+ return new HttpResponseException ( new HttpResponseMessage ( HttpStatusCode . InternalServerError )
92+ {
93+ ReasonPhrase = message ,
94+ } ) ;
95+ case SimulatedErrorType . HttpResponseException503 :
96+ return new HttpResponseException ( new HttpResponseMessage ( HttpStatusCode . ServiceUnavailable )
97+ {
98+ ReasonPhrase = message ,
99+ } ) ;
100+ case SimulatedErrorType . Exception :
101+ return new Exception ( message ) ;
102+ case SimulatedErrorType . UserSafeException :
103+ return new UserSafeException ( message ) ;
104+ case SimulatedErrorType . ReadOnlyMode :
105+ return new ReadOnlyModeException ( message ) ;
106+ default :
107+ return new Exception ( "Unknown simulated error type." ) ;
108+ }
109+ }
110+ }
111+ }
0 commit comments