public HttpResponseMessage GetInjectedServerError()

in Microsoft.Azure.Cosmos/FaultInjection/src/implementation/FaultInjectionServerErrorResultInternal.cs [281:516]


        public HttpResponseMessage GetInjectedServerError(DocumentServiceRequest dsr, string ruleId)
        {
            HttpResponseMessage httpResponse;
            //Global or Local lsn?
            string lsn = dsr.RequestContext.QuorumSelectedLSN.ToString(CultureInfo.InvariantCulture);
            INameValueCollection headers = dsr.Headers;

            switch (this.serverErrorType)
            {
                case FaultInjectionServerErrorType.Gone:
                    
                    httpResponse = new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.Gone,
                        Content = new FauntInjectionHttpContent(
                        new MemoryStream(
                            FaultInjectionResponseEncoding.GetBytes($"Fault Injection Server Error: Gone, rule: {ruleId}"))),
                    };

                    foreach (string header in headers.AllKeys())
                    {
                        httpResponse.Headers.Add(header, headers.Get(header));
                    }

                    httpResponse.Headers.Add(
                        WFConstants.BackendHeaders.SubStatus,
                        ((int)SubStatusCodes.ServerGenerated410).ToString(CultureInfo.InvariantCulture));
                    httpResponse.Headers.Add(WFConstants.BackendHeaders.LocalLSN, lsn);
                    return httpResponse;

                case FaultInjectionServerErrorType.TooManyRequests:
                    
                    httpResponse = new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.TooManyRequests,
                        Content = new FauntInjectionHttpContent(
                            new MemoryStream(
                                FaultInjectionResponseEncoding.GetBytes($"Fault Injection Server Error: TooManyRequests, rule: {ruleId}"))),
                    };


                    foreach (string header in headers.AllKeys())
                    {
                        httpResponse.Headers.Add(header, headers.Get(header));
                    }

                    httpResponse.Headers.RetryAfter = new RetryConditionHeaderValue(TimeSpan.FromMilliseconds(500));
                    httpResponse.Headers.Add(
                        WFConstants.BackendHeaders.SubStatus, 
                        ((int)SubStatusCodes.RUBudgetExceeded).ToString(CultureInfo.InvariantCulture));
                    httpResponse.Headers.Add(WFConstants.BackendHeaders.LocalLSN, lsn);

                    return httpResponse;

                case FaultInjectionServerErrorType.Timeout:
                    
                    httpResponse = new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.RequestTimeout,
                        Content = new FauntInjectionHttpContent(
                            new MemoryStream(
                                FaultInjectionResponseEncoding.GetBytes($"Fault Injection Server Error: Timeout, rule: {ruleId}"))),
                    };

                    foreach (string header in headers.AllKeys())
                    {
                        httpResponse.Headers.Add(header, headers.Get(header));
                    }

                    httpResponse.Headers.Add(
                        WFConstants.BackendHeaders.SubStatus,
                        ((int)SubStatusCodes.Unknown).ToString(CultureInfo.InvariantCulture));
                    httpResponse.Headers.Add(WFConstants.BackendHeaders.LocalLSN, lsn);

                    return httpResponse;

                case FaultInjectionServerErrorType.InternalServerError:
                    
                    httpResponse = new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.InternalServerError,
                        Content = new FauntInjectionHttpContent(
                            new MemoryStream(
                                FaultInjectionResponseEncoding.GetBytes($"Fault Injection Server Error: Internal Server Error, rule: {ruleId}"))),
                    };

                    foreach (string header in headers.AllKeys())
                    {
                        httpResponse.Headers.Add(header, headers.Get(header));
                    }

                    httpResponse.Headers.Add(
                        WFConstants.BackendHeaders.SubStatus,
                        ((int)SubStatusCodes.Unknown).ToString(CultureInfo.InvariantCulture));
                    httpResponse.Headers.Add(WFConstants.BackendHeaders.LocalLSN, lsn);

                    return httpResponse;

                case FaultInjectionServerErrorType.ReadSessionNotAvailable:
                    
                    const string badSesstionToken = "1:1#1#1=1#1=1";
                    httpResponse = new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.NotFound,
                        Content = new FauntInjectionHttpContent(
                            new MemoryStream(
                                FaultInjectionResponseEncoding.GetBytes($"Fault Injection Server Error: Read Session Not Available, rule: {ruleId}"))),
                    };

                    foreach (string header in headers.AllKeys())
                    {
                        httpResponse.Headers.Add(header, headers.Get(header));
                    }

                    httpResponse.Headers.Add(
                        WFConstants.BackendHeaders.SubStatus,
                        ((int)SubStatusCodes.ReadSessionNotAvailable).ToString(CultureInfo.InvariantCulture));
                    httpResponse.Headers.Add(HttpConstants.HttpHeaders.SessionToken, badSesstionToken);
                    httpResponse.Headers.Add(WFConstants.BackendHeaders.LocalLSN, lsn);

                    return httpResponse;

                case FaultInjectionServerErrorType.PartitionIsMigrating:
                    
                    httpResponse = new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.Gone,
                        Content = new FauntInjectionHttpContent(
                            new MemoryStream(
                                FaultInjectionResponseEncoding.GetBytes($"Fault Injection Server Error: PartitionIsMigrating, rule: {ruleId}"))),
                    };

                    foreach (string header in headers.AllKeys())
                    {
                        httpResponse.Headers.Add(header, headers.Get(header));
                    }

                    httpResponse.Headers.Add(
                        WFConstants.BackendHeaders.SubStatus,
                        ((int)SubStatusCodes.CompletingPartitionMigration).ToString(CultureInfo.InvariantCulture));
                    httpResponse.Headers.Add(WFConstants.BackendHeaders.LocalLSN, lsn);

                    return httpResponse;

                case FaultInjectionServerErrorType.PartitionIsSplitting:
                    
                    httpResponse = new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.Gone,
                        Content = new FauntInjectionHttpContent(
                            new MemoryStream(
                                FaultInjectionResponseEncoding.GetBytes($"Fault Injection Server Error: PartitionIsSplitting, rule: {ruleId}"))),
                    };

                    foreach (string header in headers.AllKeys())
                    {
                        httpResponse.Headers.Add(header, headers.Get(header));
                    }

                    httpResponse.Headers.Add(
                        WFConstants.BackendHeaders.SubStatus,
                        ((int)SubStatusCodes.CompletingSplit).ToString(CultureInfo.InvariantCulture));
                    httpResponse.Headers.Add(WFConstants.BackendHeaders.LocalLSN, lsn);

                    return httpResponse;

                case FaultInjectionServerErrorType.ServiceUnavailable:

                    httpResponse = new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.ServiceUnavailable,
                        Content = new FauntInjectionHttpContent(
                            new MemoryStream(
                                FaultInjectionResponseEncoding.GetBytes($"Fault Injection Server Error: Service Unavailable, rule: {ruleId}"))),
                    };

                    foreach (string header in headers.AllKeys())
                    {
                        httpResponse.Headers.Add(header, headers.Get(header));
                    }

                    httpResponse.Headers.Add(
                        WFConstants.BackendHeaders.SubStatus,
                        ((int)SubStatusCodes.Unknown).ToString(CultureInfo.InvariantCulture));
                    httpResponse.Headers.Add(WFConstants.BackendHeaders.LocalLSN, lsn);

                    return httpResponse;

                case FaultInjectionServerErrorType.DatabaseAccountNotFound:
                    
                    httpResponse = new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.Forbidden,
                        Content = new FauntInjectionHttpContent(
                            new MemoryStream(
                                FaultInjectionResponseEncoding.GetBytes($"Fault Injection Server Error: DatabaseAccountNotFound, rule: {ruleId}"))),
                    };

                    foreach (string header in headers.AllKeys())
                    {
                        httpResponse.Headers.Add(header, headers.Get(header));
                    }

                    httpResponse.Headers.Add(
                        WFConstants.BackendHeaders.SubStatus,
                        ((int)SubStatusCodes.DatabaseAccountNotFound).ToString(CultureInfo.InvariantCulture));
                    httpResponse.Headers.Add(WFConstants.BackendHeaders.LocalLSN, lsn);

                    return httpResponse;

                case FaultInjectionServerErrorType.LeaseNotFound:

                    httpResponse = new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.Gone,
                        Content = new FauntInjectionHttpContent(
                            new MemoryStream(
                                FaultInjectionResponseEncoding.GetBytes($"Fault Injection Server Error: LeaseNotFound, rule: {ruleId}"))),
                    };

                    foreach (string header in headers.AllKeys())
                    {
                        httpResponse.Headers.Add(header, headers.Get(header));
                    }

                    httpResponse.Headers.Add(
                        WFConstants.BackendHeaders.SubStatus,
                        ((int)SubStatusCodes.LeaseNotFound).ToString(CultureInfo.InvariantCulture));
                    httpResponse.Headers.Add(WFConstants.BackendHeaders.LocalLSN, lsn);

                    return httpResponse;

                default:
                    throw new ArgumentException($"Server error type {this.serverErrorType} is not supported");
            }
        }