Skip to content

Commit

Permalink
Respond appropiately to unsupport content format option
Browse files Browse the repository at this point in the history
  • Loading branch information
NZSmartie committed Sep 14, 2017
1 parent aca3e39 commit 0c1e1cf
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion CoAPNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoAPNet.Udp", "CoAPNet.Udp\CoAPNet.Udp.csproj", "{5A4DEFAA-29CB-432F-83BF-5970304DCD6C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoAPNet.Server", "CoAPNet.Server\CoAPNet.Server.csproj", "{0484D42B-8BC9-42E4-9C70-DB505779F1E0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoAPNet.Server", "CoAPNet.Server\CoAPNet.Server.csproj", "{0484D42B-8BC9-42E4-9C70-DB505779F1E0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
24 changes: 21 additions & 3 deletions CoAPNet/CoapException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,31 @@ namespace CoAPNet
[ExcludeFromCodeCoverage]
public class CoapException : Exception
{
public CoapMessageCode ResponseCode { get; }

public CoapException()
{ }
{
ResponseCode = CoapMessageCode.InternalServerError;
}

public CoapException(string message) : base(message)
{ }
{
ResponseCode = CoapMessageCode.InternalServerError;
}

public CoapException(string message, CoapMessageCode responseCode) : base(message)
{
ResponseCode = responseCode;
}

public CoapException(string message, Exception innerException) : base(message, innerException)
{ }
{
ResponseCode = CoapMessageCode.InternalServerError;
}

public CoapException(string message, Exception innerException, CoapMessageCode responseCode) : base(message, innerException)
{
ResponseCode = responseCode;
}
}
}
5 changes: 5 additions & 0 deletions CoAPNet/CoapMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,16 @@ public static bool IsServerError(this CoapMessageCode code)

[ExcludeFromCodeCoverage]
public class CoapMessageFormatException : CoapException {

public CoapMessageFormatException() :base() { }

public CoapMessageFormatException(string message) : base(message) { }

public CoapMessageFormatException(string message, CoapMessageCode responseCode) : base(message, responseCode) { }

public CoapMessageFormatException(string message, Exception innerException) : base(message, innerException) { }

public CoapMessageFormatException(string message, Exception innerException, CoapMessageCode responseCode, responseCode) : base(message, innerException) { }
}

public class CoapMessage
Expand Down
4 changes: 2 additions & 2 deletions CoAPNet/CoapOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public class CoapOptionException : CoapException
{
public CoapOptionException() : base() { }

public CoapOptionException(string message) : base(message) { }
public CoapOptionException(string message) : base(message, CoapMessageCode.BadOption) { }

public CoapOptionException(string message, Exception innerException) : base(message, innerException) { }
public CoapOptionException(string message, Exception innerException) : base(message, innerException, CoapMessageCode.BadOption) { }
}

public enum OptionType
Expand Down
6 changes: 3 additions & 3 deletions CoAPNet/Options/Content.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ public static implicit operator ContentFormatType(string name)
{
if (nameLookup.TryGetValue(name, out var result))
return result;
throw new InvalidCastException();
throw new CoapOptionException($"Unsupported content format \"{name}\"", CoapMessageCode.BadOption);
}

public static implicit operator ContentFormatType(int value)
{
if (valueLookup.TryGetValue(value, out var result))
return result;
throw new InvalidCastException();
throw new CoapOptionException($"Unsupported content format ({value})", CoapMessageCode.BadOption);
}

public static implicit operator ContentFormatType(uint value)
{
return (ContentFormatType)((int)value);
return ((int)value);
}

public static implicit operator int(ContentFormatType type)
Expand Down
4 changes: 2 additions & 2 deletions CoAPNet/Utils/CoapMessageUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public static CoapMessage FromException(Exception exception)

switch (exception)
{
case CoapOptionException _:
result.Code = CoapMessageCode.BadOption;
case CoapException coapEx:
result.Code = coapEx.ResponseCode;
break;
case NotImplementedException _:
result.Code = CoapMessageCode.NotImplemented;
Expand Down

0 comments on commit 0c1e1cf

Please sign in to comment.