Example #1
0
        public async Task Should_Throw_ChatNotFoundException_For_Non_Existent_Chat()
        {
            ChatNotFoundException exception = await Assert.ThrowsAsync <ChatNotFoundException>(
                async() => await BotClient.SendTextMessageAsync(0, "test")
                );

            Assert.Equal(400, exception.ErrorCode);
            Assert.Contains("chat not found", exception.Message);
        }
Example #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            /*
             * if (env.IsDevelopment())
             * {
             *  app.UseDeveloperExceptionPage();
             * }
             * else
             * {
             *  // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
             *  app.UseHsts();
             * }
             */
            app.UseExceptionHandler(errorApp =>
            {
                errorApp.Run(async context =>
                {
                    var exceptionHandlerPathFeature =
                        context.Features.Get <IExceptionHandlerPathFeature>();

                    string text = exceptionHandlerPathFeature?.Error switch
                    {
                        UserNotFoundException e => "User not found",
                        UserNotHaveEnoughRightsException e => "User not have enough rights",
                        UserAlreadyInChatException e => "User already in chat",
                        ChatNotFoundException e => "Chat not found",
                        UsernameAlreadyExistsException e => "Username already exists",
                        _ => "Unknown error"
                    };
                    context.Response.ContentType = MediaTypeNames.Application.Json;
                    context.Response.StatusCode  = 400;
                    await context.Response.WriteAsync(JsonConvert.SerializeObject(new ApiError {
                        Error = text
                    }));
                });
            });

            app.UseOpenApi(settings =>
            {
                settings.Path         = $"{ApiConstant.Prefix}openapi/swagger.json";
                settings.DocumentName = "openapi";
            });

            app.UseSwaggerUi3(options =>
            {
                options.Path         = $"{ApiConstant.Prefix}openapi";
                options.DocumentPath = $"{ApiConstant.Prefix}openapi/swagger.json";
            });

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }