Created
October 23, 2024 20:24
-
-
Save almeidaalex/bce1c116382907af907177fb6f2598ee to your computer and use it in GitHub Desktop.
Exception API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build | |
WORKDIR /app | |
# Copy the project file and restore dependencies | |
COPY *.csproj ./ | |
RUN dotnet restore | |
# Copy the rest of the application and build it | |
COPY . ./ | |
RUN dotnet publish -c Release -o out | |
# Use the official .NET runtime image to run the application | |
FROM mcr.microsoft.com/dotnet/aspnet:9.0 | |
WORKDIR /app | |
COPY --from=build /app/out . | |
# Specify the entry point for the application | |
ENTRYPOINT ["dotnet", "ExceptionTest.dll"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Components.Forms; | |
var builder = WebApplication.CreateBuilder(args); | |
var app = builder.Build(); | |
app.MapGet("/no-exception/{id}", (int id) => { | |
return id % 2 == 0 ? Results.BadRequest("id cannot be even") : Results.Ok("id is valid"); | |
}); | |
app.MapGet("/exception/{id}", (int id) => { | |
return id % 2 == 0 ? throw new Exception("id cannot be even") : Results.Ok("id is valid"); | |
}); | |
app.MapGet("/custom-exception/{id}", (int id) => { | |
var handler = new HandleTheNumber(id); | |
try | |
{ | |
handler.ExceptionHandler(); | |
return Results.Ok("id is valid"); | |
} | |
catch (CustomException ex) | |
{ | |
return Results.InternalServerError(ex.Message); | |
} | |
}); | |
app.MapGet("/notification/{id}", (int id) => { | |
var handler = new HandleTheNumber(id); | |
var result = handler.NoExceptionHandler(); | |
if (result.IsRight) | |
{ | |
return Results.Ok("id is valid"); | |
} | |
else | |
{ | |
return Results.BadRequest("id cannot be even"); | |
} | |
}); | |
app.Run(); | |
class CustomException(string message) : Exception(message) | |
{ | |
} | |
class HandleTheNumber(int number) | |
{ | |
private readonly int number = number; | |
public void ExceptionHandler() | |
{ | |
if (this.number % 2 == 0) | |
{ | |
throw new CustomException("number cannot be even"); | |
} | |
} | |
public Either<string, string> NoExceptionHandler() | |
{ | |
return this.number % 2 == 0 ? | |
Either<string, string>.Left("number cannot be even") : | |
Either<string, string>.Right("number is valid"); | |
} | |
} | |
public record Either<TLeft, TRight> | |
{ | |
private readonly TLeft? left; | |
private readonly TRight? right; | |
private readonly bool isLeft; | |
private Either(TLeft left) | |
{ | |
this.left = left; | |
this.right = default; | |
this.isLeft = true; | |
} | |
private Either(TRight right) | |
{ | |
this.left = default; | |
this.right = right; | |
this.isLeft = false; | |
} | |
public static Either<TLeft, TRight> Left(TLeft value) => new(value); | |
public static Either<TLeft, TRight> Right(TRight value) => new(value); | |
public TResult Match<TResult>( | |
Func<TLeft, TResult> leftFunc, | |
Func<TRight, TResult> rightFunc | |
) => isLeft ? leftFunc(left!) : rightFunc(right!); | |
public void Match( | |
Action<TLeft> leftAction, | |
Action<TRight> rightAction | |
) | |
{ | |
if (isLeft) | |
leftAction(left!); | |
else | |
rightAction(right!); | |
} | |
public bool IsLeft => isLeft; | |
public bool IsRight => !isLeft; | |
public TLeft LeftOrDefault(TLeft defaultValue = default!) => | |
isLeft ? left! : defaultValue; | |
public TRight RightOrDefault(TRight defaultValue = default!) => | |
!isLeft ? right! : defaultValue; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http from 'k6/http'; | |
import { check } from 'k6'; | |
const params = JSON.parse(__ENV.PARAMS || '{}'); | |
const endpoint = params.endpoint || ''; | |
export let options = { | |
vus: 10, // Number of virtual users | |
iterations: 1000, // Total number of requests | |
}; | |
export default function () { | |
let iterationNumber = __ITER; | |
let url = `http://localhost:8080/${endpoint}/${iterationNumber}`; | |
let res = http.get(url); | |
check(res, { | |
'status is 200': (r) => r.status === 200, | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment