Skip to content

Commit

Permalink
Add error codes for "exceeds length" and "db exists"
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter-B- committed Feb 18, 2024
1 parent 3594623 commit 8f4f584
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 77 deletions.
102 changes: 102 additions & 0 deletions SoftPlc/Exceptions/DbAccessException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
namespace SoftPlc.Exceptions;

public abstract class DbAccessException : Exception
{
protected DbAccessException(string message, Exception? innerException = null) : base(message, innerException)
{
}

public abstract int StatusCode { get; }
public abstract string Title { get; }
}

public class InvalidDbSizeException : DbAccessException
{
public InvalidDbSizeException(int size) : base($"DB size must be > 0 but was {size}.")
{
Size = size;
}

public int Size { get; }

public override int StatusCode => StatusCodes.Status400BadRequest;
public override string Title => "Invalid DB size";

public static void ThrowIfInvalid(int size)
{
if (size < 1)
throw new InvalidDbSizeException(size);
}
}

public class DbOutOfRangeException : DbAccessException
{
public DbOutOfRangeException(string message, int dbNo, Exception? innerException = null) : base(message, innerException)
{
DbNo = dbNo;
}

public DbOutOfRangeException(int dbNo) : base($"DB number must be positive, but is {dbNo}.")
{
DbNo = dbNo;
}

public int DbNo { get; }

public override int StatusCode => StatusCodes.Status400BadRequest;
public override string Title => "DB out of range";

public static void ThrowIfInvalid(int dbNo)
{
if (dbNo < 1)
throw new DbOutOfRangeException(dbNo);
}
}

public class DbNotFoundException : DbAccessException
{
public DbNotFoundException(int dbNo) : base($"DB {dbNo} not found.")
{
DbNo = dbNo;
}

public DbNotFoundException(string message, int dbNo, Exception? innerException = null) : base(message, innerException)
{
DbNo = dbNo;
}

public int DbNo { get; }

public override int StatusCode => StatusCodes.Status404NotFound;
public override string Title => "DB not found";
}

public class DbExistsException : DbAccessException
{
public DbExistsException(int dbNo) : base($"DB {dbNo} already exists.")
{
DbNo = dbNo;
}

public int DbNo { get; }

public override int StatusCode => StatusCodes.Status409Conflict;
public override string Title => "DB already exists";
}

public class DateExceedsDbLengthException : DbAccessException
{
public DateExceedsDbLengthException(int dbNo, int dbLength, int dataLength) : base($"Data with {dataLength} bytes exceeds length of DB {dbNo} of {dbLength} bytes.")
{
DbNo = dbNo;
DBLength = dbLength;
DataLength = dataLength;
}

public int DataLength { get; }
public int DBLength { get; }
public int DbNo { get; }

public override int StatusCode => StatusCodes.Status400BadRequest;
public override string Title => "Data exceeds DB length";
}
37 changes: 10 additions & 27 deletions SoftPlc/Exceptions/DbAccessExceptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,20 @@ public async ValueTask<bool> TryHandleAsync(
Exception exception,
CancellationToken cancellationToken)
{
if (exception is DbNotFoundException dbNotFound)
{
var problemDetails = new ProblemDetails
{
Status = StatusCodes.Status404NotFound,
Title = "DB not found",
Detail = dbNotFound.Message
};

httpContext.Response.StatusCode = problemDetails.Status.Value;
if (exception is not DbAccessException dbAccessException)
return false;

await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
return true;
}

if (exception is DbOutOfRangeException outOfRange)
{
var problemDetails = new ProblemDetails
var problemDetails =
new ProblemDetails
{
Status = StatusCodes.Status400BadRequest,
Title = "DB out of range",
Detail = outOfRange.Message
Status = dbAccessException.StatusCode,
Title = dbAccessException.Title,
Detail = dbAccessException.Message
};

httpContext.Response.StatusCode = problemDetails.Status.Value;

await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
return true;
}

httpContext.Response.StatusCode = problemDetails.Status.Value;

return false;
await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
return true;
}
}
44 changes: 0 additions & 44 deletions SoftPlc/Exceptions/DbAccessExceptions.cs

This file was deleted.

14 changes: 8 additions & 6 deletions SoftPlc/Services/PlcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,12 @@ public void AddDatablock(int id, int size)
{
CheckServerRunning();
DbOutOfRangeException.ThrowIfInvalid(id);
InvalidDbSizeException.ThrowIfInvalid(size);

if (size < 1) throw new ArgumentException("Invalid size for datablock - size must be > 1", nameof(size));
if (datablocks.ContainsKey(id)) throw new InvalidOperationException($"A Datablock with id = {id} already exists");
var db = new DatablockDescription(id, size);
while(!datablocks.TryAdd(id, db)){ }
if (!datablocks.TryAdd(id, db))
throw new DbExistsException(id);

server.RegisterArea(S7Server.srvAreaDB, id, ref datablocks[id].Data, datablocks[id].Data.Length);
}

Expand All @@ -150,9 +151,10 @@ public void UpdateDatablockData(int id, byte[] data)
if (!datablocks.TryGetValue(id, out var db))
throw new DbNotFoundException(id);

if (data != null && data.Length > db.Data.Length) throw new ArgumentException("Too much data as expected", nameof(data));
if(data != null)
Array.Copy(data, datablocks[id].Data, data.Length);
if (data.Length > db.Data.Length)
throw new DateExceedsDbLengthException(id, db.Data.Length, data.Length);

Array.Copy(data, datablocks[id].Data, data.Length);
}

public void RemoveDatablock(int id)
Expand Down

0 comments on commit 8f4f584

Please sign in to comment.