Ignore:
Timestamp:
03/22/26 17:58:40 (4 months ago)
Author:
kikisrbinoska <srbinoskakristina07@…>
Branches:
main
Children:
73b69b2
Parents:
b62cefc
Message:

Added fixes for the login,stories management and reading lists

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ChapterX.API/Program.cs

    rb62cefc racf690c  
    1616});
    1717
    18 builder.Services.AddControllers();
     18builder.Services.AddControllers()
     19    .AddJsonOptions(options =>
     20    {
     21        options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
     22    });
    1923builder.Services.AddEndpointsApiExplorer();
    2024builder.Services.AddSwaggerGen(options =>
     
    6872var app = builder.Build();
    6973
     74app.UseCors("Frontend");
     75
    7076if (app.Environment.IsDevelopment())
    7177{
     
    7480}
    7581
    76 app.UseHttpsRedirection();
    77 app.UseCors("Frontend");
     82app.UseExceptionHandler(err => err.Run(async ctx =>
     83{
     84    var ex = ctx.Features.Get<Microsoft.AspNetCore.Diagnostics.IExceptionHandlerFeature>()?.Error;
     85    ctx.Response.ContentType = "application/json";
     86
     87    string message;
     88    int status;
     89
     90    if (ex is UnauthorizedAccessException)
     91    {
     92        status = 401;
     93        message = ex.Message;
     94    }
     95    else if (ex is InvalidOperationException)
     96    {
     97        status = 400;
     98        message = ex.Message;
     99    }
     100    else if (ex is Microsoft.EntityFrameworkCore.DbUpdateException dbEx)
     101    {
     102        status = 400;
     103        var inner = dbEx.InnerException?.Message ?? "";
     104        if (inner.Contains("email_format"))
     105            message = "Invalid email format.";
     106        else if (inner.Contains("unique") || inner.Contains("duplicate") || inner.Contains("23505"))
     107            message = "A user with this email or username already exists.";
     108        else
     109            message = "Database error: " + inner;
     110    }
     111    else
     112    {
     113        status = 500;
     114        message = ex?.Message ?? "An error occurred.";
     115    }
     116
     117    ctx.Response.StatusCode = status;
     118    await ctx.Response.WriteAsJsonAsync(new { message });
     119}));
     120
    78121app.UseAuthentication();
    79122app.UseAuthorization();
Note: See TracChangeset for help on using the changeset viewer.