I'm a beginner in ASP.NET Core, just a question on ObjectResult. I saw sone code like this:
public override void OnException(ExceptionContext context)
{
var myerror = new
{
Success = false,
Errors = new [] { context.Exception.Message }
};
context.Result = new ObjectResult(myerror)
{
StatusCode = 500
};
context.ExceptionHandled = true;
...
}
my questions are:
1- is the property 'Errors' anonymous type of object "myerror" creating the response body with the exception's message?
It seems that I can use any names for properties not just 'Success; and 'Errors' so can I code like:
var myerror = new
{
mySuccess = false,
myErrors = new [] { context.Exception.Message }
};
is it OK?
whats the purpose of doing context.ExceptionHandled = true;? the book says Marks the exception as handled to prevent it propagating out of MvcMiddleware. But why it need to prevent it propagating out?
1) Yes.
2) Yes, it is okay. You can create object of any structure you want, not only anonymous objects.
public class ErrorModel
{
public string Error { get; set; }
public int Id { get; set; }
public List<int> Values { get; set; }
}
//filter
var error = new ErrorModel
{
Error = context.Exception.Message,
Id = 1,
Values = new List<int> { 1, 2, 3 }
};
context.Result = new ObjectResult(error)
{
StatusCode = 500
};
3) It is possible that there are multiple exception filters in your application and if you don't set ExceptionHandled to true while processing an exception then every filter get called and Result is overriden. The purpose of this property is to indicate that certain filter was able to cope with exception and there is no need to run other exception filters. It is useful in scenarious when filter can handle only certain types of exception.
//handles only exceptions caused by dividing by zero
public class DivideByZeroExceptionFilterAttribute : Attribute, IExceptionFilter
{
public void OnException(ExceptionContext context)
{
//chech if this is divide by zero exception
if (!(context.Exception is DivideByZeroException))
return;
var myerror = new
{
result = false,
message = "Division by zero went wrong"
};
context.Result = new ObjectResult(myerror)
{
StatusCode = 500
};
//set "handled" to true since exception is already property handled
//and there is no need to run other filters
context.ExceptionHandled = true;
}
}
Related
I'm looking for a way to add error code alongside the error message to ModelState.
for example
ModelState.AddModelError("ErrorKey", new { Code = 4001, Message = "Some error message" });
For some bad requests client should do an action and comparing error message is not an ideal solution for making a decision. ModelState.AddModelError method only accepts two parameters, an error key and a message. Is there a way to achieve this or something similar?
No, there is not a way to achieve what you are looking for, in your code when you're trying to do something like this:
return BadRequest(ModelState);
You’ll receive a 400 bad request response back with message you've already added (as you can see, the error code has been presented already here). So, there is neither a usage nor a way of adding the Error Code in your case.
I found a way to add the error code to ValidationProblemDetails:
public class CustomValidationProblemDetails : ValidationProblemDetails
{
public CustomValidationProblemDetails()
{
}
[JsonPropertyName("errors")]
public new IEnumerable<ValidationError> Errors { get; } = new List<ValidationError>();
}
ValidationProblemDetails has an Error property that is IDictionary<string, string[]> and replace this property with our version to add code error.
public class ValidationError
{
public int Code { get; set; }
public string Message { get; set; }
}
Constructor of ValidationProblemDetails accepts ModelStateDictionary and need to convert it to list of ValidationError:
public CustomValidationProblemDetails(IEnumerable<ValidationError> errors)
{
Errors = errors;
}
public CustomValidationProblemDetails(ModelStateDictionary modelState)
{
Errors = ConvertModelStateErrorsToValidationErrors(modelState);
}
private List<ValidationError> ConvertModelStateErrorsToValidationErrors(ModelStateDictionary modelStateDictionary)
{
List<ValidationError> validationErrors = new();
foreach (var keyModelStatePair in modelStateDictionary)
{
var errors = keyModelStatePair.Value.Errors;
switch (errors.Count)
{
case 0:
continue;
case 1:
validationErrors.Add(new ValidationError { Code = 100, Message = errors[0].ErrorMessage });
break;
default:
var errorMessage = string.Join(Environment.NewLine, errors.Select(e => e.ErrorMessage));
validationErrors.Add(new ValidationError { Message = errorMessage });
break;
}
}
return validationErrors;
}
Create custom ProblemDetailsFactory to create CustomValidationProblemDetails when we want to return bad request response:
public class CustomProblemDetailsFactory : ProblemDetailsFactory
{
public override ProblemDetails CreateProblemDetails(HttpContext httpContext, int? statusCode = null, string title = null,
string type = null, string detail = null, string instance = null)
{
var problemDetails = new ProblemDetails
{
Status = statusCode,
Title = title,
Type = type,
Detail = detail,
Instance = instance,
};
return problemDetails;
}
public override ValidationProblemDetails CreateValidationProblemDetails(HttpContext httpContext,
ModelStateDictionary modelStateDictionary, int? statusCode = null, string title = null, string type = null,
string detail = null, string instance = null)
{
statusCode ??= 400;
type ??= "https://tools.ietf.org/html/rfc7231#section-6.5.1";
instance ??= httpContext.Request.Path;
var problemDetails = new CustomValidationProblemDetails(modelStateDictionary)
{
Status = statusCode,
Type = type,
Instance = instance
};
if (title != null)
{
// For validation problem details, don't overwrite the default title with null.
problemDetails.Title = title;
}
var traceId = Activity.Current?.Id ?? httpContext?.TraceIdentifier;
if (traceId != null)
{
problemDetails.Extensions["traceId"] = traceId;
}
return problemDetails;
}
}
And at the end register the factory:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddTransient<ProblemDetailsFactory, CustomProblemDetailsFactory>();
}
Read the Extending ProblemDetails - Add error code to ValidationProblemDetails for more detail.
I need to define a string array type dataType in Grpc message. not sure how to do. right now i am doing it as a
repeated string Title= 1,
here i need name field as string array Type. But it is showing error that it is, field is readonly type when bind data in it:
public override async Task<UserResponse> CreateUser(
UserModel request, ServerCallContext context)
{
var eventResponse = new UserResponse();
var createCmd = new CreateUserCommand
{
Model = new UserDto
{
Title = request.Title,
Id = request.Id,
}
}
}
here in title i need to bind data
The generated code from protoc here gives you something like:
private readonly RepeatedField<string> title_ = new RepeatedField<string>();
[DebuggerNonUserCodeAttribute]
public RepeatedField<string> Title {
get { return title_; }
}
So: Title is indeed read-only. This means that instead of assigning it, you should explore what APIs exist for adding to it - i.e.
var user = new UserDto
{
Id = request.Id,
}
user.Title.Add(request.Title);
// or AddRange, etc
You may still be able to use initializer syntax, too:
new UserDto
{
Id = request.Id,
Title = { request.Title }
}
(which is an .Add)
Trying to create kind of market scanner. Code below is supposed to return chain of option contracts. Call to TWS API is an async method that returns some data only if I get ContractEnd or Error response from TWS. On the first call to reqContractDetails() it works as expected, I get list of contracts, receive message "ContractEnd", and exit from the method.
Obstacle
In some reason, on the second call to reqContractDetails() I don't get any notification from TWS. I have to stop and restart my application, initiating new connection to the server to make it working again.
Update
After refactoring my code I'm getting an error on a second call that says "Unable to read beyond the end of the stream". Call stack looks this way.
IBLibrary.dll!IBLibrary.OptionService.GetOptionsChain.AnonymousMethod__3(IBLibrary.Messages.ErrorMessage data) Line 64
IBLibrary.dll!IBLibrary.Classes.Client.error(string str) Line 42
CSharpAPI.dll!IBApi.EReader.putMessageToQueue() Line 94
CSharpAPI.dll!IBApi.EReader.Start.AnonymousMethod__9_0() Line 48
My implementation of the wrapper in C#
public class BaseService : IDisposable
{
protected Client Sender { get; set; }
protected EReader Receiver { get; set; }
public BaseService()
{
Sender = new Client();
Sender.Socket.eConnect("127.0.0.1", 7496, 0);
Receiver = new EReader(Sender.Socket, Sender.Signal);
Receiver.Start();
var process = new Thread(() =>
{
while (Sender.Socket.IsConnected())
{
Sender.Signal.waitForSignal();
Receiver.processMsgs();
}
})
{
IsBackground = true
};
process.Start();
}
public void Dispose()
{
Sender.Socket.eDisconnect();
}
}
public class OptionService : BaseService
{
public Task<List<OptionModel>> GetOptionsChain(OptionModel query)
{
if (query == null)
{
query = new OptionModel();
}
var process = Task.Run(() =>
{
var done = false;
var id = new Random(DateTime.Now.Millisecond).Next();
var contract = new Contract
{
Symbol = query.Symbol,
SecType = "OPT",
Exchange = "SMART",
Currency = "USD",
LastTradeDateOrContractMonth = query.Expiration
};
var contracts = new List<OptionModel>();
Action<ErrorMessage> errorMessage = null;
Action<ContractDetailsMessage> contractMessage = null;
Action<ContractDetailsEndMessage> contractMessageEnd = null;
contractMessage = (ContractDetailsMessage data) =>
{
contracts.Add(new OptionModel
{
Symbol = data.ContractDetails.Contract.Symbol,
Right = data.ContractDetails.Contract.Right,
Strike = data.ContractDetails.Contract.Strike,
Expiration = data.ContractDetails.RealExpirationDate
});
};
// I receive this message at first, but not the second time
contractMessageEnd = (ContractDetailsEndMessage data) =>
{
done = true;
};
errorMessage = (ErrorMessage data) =>
{
var notifications = new List<int>
{
(int) ErrorCode.MarketDataFarmConnectionIsOK,
(int) ErrorCode.HmdsDataFarmConnectionIsOK
};
if (notifications.Contains(data.ErrorCode) == false)
{
done = true;
}
};
Sender.ErrorEvent += errorMessage;
Sender.ContractDetailsEvent += contractMessage;
Sender.ContractDetailsEndEvent += contractMessageEnd;
Sender.Socket.reqContractDetails(id, contract);
// Execute method until we get all contracts
// The econd call to reqContractDetails doesn't return
// any notification, so obviously this line hangs forever
while (done == false);
Sender.ErrorEvent -= errorMessage;
Sender.ContractDetailsEvent -= contractMessage;
Sender.ContractDetailsEndEvent -= contractMessageEnd;
return contracts;
});
return process;
}
}
As far as nobody has the answer, even IB itself, the only solution that I see is, to convert my API controller to a synchronous controller and close socket connection to IB server after every request.
Old version.
public class ServiceOptionsController : BaseServiceController
{
OptionService Service = new OptionService();
[AcceptVerbs("POST")]
public async Task<List<OptionModel>> Options([FromBody] dynamic data)
{
var selectors = data.ToObject<QueryModel>();
var optionModel = new OptionModel
{
Symbol = "MSFT",
Expiration = "201806"
};
var processes = new List<Task<List<OptionModel>>>
{
Service.GetOptionsChain(optionModel)
};
return (await Task.WhenAll(processes)).SelectMany(o => o).ToList();
}
}
Working version.
public class ServiceOptionsController : BaseServiceController
{
[AcceptVerbs("POST")]
public List<OptionModel> Options([FromBody] dynamic data)
{
var selectors = data.ToObject<QueryModel>();
var optionModel = new OptionModel
{
Symbol = "MSFT",
Expiration = "201806"
};
var optionService = new OptionService();
var processes = new List<Task<List<OptionModel>>>
{
optionService.GetOptionsChain(optionModel)
};
var items = Task.WhenAll(processes).Result.SelectMany(o => o).ToList();
optionService.Dispose(); // Ridiculous fix for ridiculous API
return items;
}
}
I am creating an extension method that performs a test on an object to see if it has a specific custom attribute.
I want to create a unit test for my extension method. How can I assert that the test in the extension method should fail?
[Test]
public void ShouldFailIfEmailAttributeMissingFromFieldName()
{
//--Arrange
var model = new { Field = 1 };
//--Act
model.ShouldValidateTheseFields(new List<FieldValidation>
{
new EmailAddressFieldValidation
{
ErrorId = 1,
ErrorMessage = "Message",
FieldName = nameof(model.Field)
}
});
//--Assert
}
Basically, the ShouldValidateTheseFields does reflection and asserts that it should have a custom attribute on the field named "Field" and I need to assert that it failed.
Catch the expected exception. If none is thrown the test fails
[Test]
public void ShouldFailIfEmailAttributeMissingFromFieldName() {
//--Arrange
var model = new { Field = 1 };
//--Act
try {
model.ShouldValidateTheseFields(new List<FieldValidation> {
new EmailAddressFieldValidation {
ErrorId = 1,
ErrorMessage = "Message",
FieldName = nameof(model.Field)
}
});
} catch(MyExpectedException e) {
return;
}
//--Assert
Assert.Fail();
}
Depending on the test framework being used there should be a way for you to assert expected exceptions for a test, which would basically follow a similar format above under the hood.
Create a new custom exception and have it throw if it is missing the custom attribute:
[Test]
public void ShouldFailIfEmailAddressAttributeIsMissingFromFieldName()
{
//--Arrange
var model = new { Field = 1 };
//--Act
Should.Throw<EmailAddressAttributeNotFoundException>(() => model.ShouldValidateTheseFields(
new List<FieldValidation>
{
new EmailAddressFieldValidation
{
ErrorId = 1,
ErrorMessage = "Message",
FieldName = nameof(model.Field)
}
}));
}
To check if an assertion fails you need to catch the Assertion exception. In this case since the Shouldly Framework is being used, it is a Shouldly.ShouldAssertException which is being thrown in the extension method:
[Test]
public void ShouldFailIfEmailAddressAttributeHasWrongErrorId()
{
//--Arrange
var model = new TestModelTwo();
//--Act
Should.Throw<ShouldAssertException>(() => model.ShouldValidateTheseFields(
new List<FieldValidation>
{
new EmailAddressFieldValidation
{
ErrorId = 2,
ErrorMessage = "Message",
FieldName = nameof(model.Field)
}
}));
}
Using the class:
public class TestModel
{
[EmailAddress(1)]
public string Field { get; set; }
}
The failing assertion in the extension method is ErrorId.ShouldBe(2) when it is actually 1 on the model.
I have the following test code:
[TestClass]
public class TestJsonDeserialize
{
public class MyClass
{
[JsonProperty("myint")]
public int MyInt { get; set; }
[JsonProperty("Mybool")]
public bool Mybool { get; set; }
}
[TestMethod]
public void Test1()
{
var errors = new List<string>();
var json1 = "{\"myint\":1554860000,\"Mybool\":false}";
var json2 = "{\"myint\":3554860000,\"Mybool\":false}";
var i = JsonConvert.DeserializeObject<MyClass>(json2, new JsonSerializerSettings
{
Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
{
Debug.WriteLine(args.ErrorContext.Error.Message);
errors.Add(args.ErrorContext.Error.Message);
args.ErrorContext.Handled = true;
}
});
Assert.IsTrue(errors.Count <= 1);
}
}
The call to JsonConvert.DeserializeObject produces 2 errors. One of them is expected, but the other not.
The errors are:
JSON integer 3554860000 is too large or small for an Int32. Path 'myint', line 1, position 19.
Unexpected token when deserializing object: Boolean. Path 'Mybool', line 1, position 34.
Why is there a 2nd error although the 1st error is marked as handled.
I already updated from Newtonsoft.Json 8.0.2 to 9.0.1 but it remains.
When passing the first string (json1 instead of json2), then no errors at all occur.
Update
Reported as Issue 1194: JsonTextReader.ParseNumber leads to error after ThrowReaderError and closed by Newtonsoft as not reproducible in then-current build which subsequently was released as Json.NET 10.0.1.
Original Answer
This may be a bug in JsonTextReader.
In JsonTextReader.ParseNumber(ReadType readType, char firstChar, int initialPosition) there is the following logic, somewhat simplified:
else if (readType == ReadType.ReadAsInt32)
{
// Snip
int value;
ParseResult parseResult = ConvertUtils.Int32TryParse(_stringReference.Chars, _stringReference.StartIndex, _stringReference.Length, out value);
if (parseResult == ParseResult.Success)
{
numberValue = value;
}
else if (parseResult == ParseResult.Overflow)
{
throw ThrowReaderError("JSON integer {0} is too large or small for an Int32.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
}
else
{
throw ThrowReaderError("Input string '{0}' is not a valid integer.".FormatWith(CultureInfo.InvariantCulture, _stringReference.ToString()));
}
}
numberType = JsonToken.Integer;
}
// Snip
// Finally, after successfully parsing the number
ClearRecentString();
// index has already been updated
SetToken(numberType, numberValue, false);
At the point the exception is thrown by ThrowReadError(), the stream position has been advanced past the too-large integer. However, the value for JsonReader.TokenType has not been updated and still returns JsonToken.PropertyName for the last token that was successfully parsed, namely the "myint" name. Later, after the exception is swallowed and ignored, the inconsistency between the stream position and current token value causes the "Mybool" property name to be skipped, leading to the second error.
If, in the debugger, when the exception is thrown I manually call
SetToken(JsonToken.Undefined);
ClearRecentString();
Then the remainder of the file can be successfully parsed. (I'm not sure JsonToken.Undefined is the right choice here.)
You might want to report an issue to Newtonsoft.
Since the JsonReader is not passed in to the error handler, the only workaround I could find is to subclass JsonTextReader as follows:
public class FixedJsonTextReader : JsonTextReader
{
public FixedJsonTextReader(TextReader reader) : base(reader) { }
public override int? ReadAsInt32()
{
try
{
return base.ReadAsInt32();
}
catch (JsonReaderException)
{
if (TokenType == JsonToken.PropertyName)
SetToken(JsonToken.None);
throw;
}
}
}
And then do:
var errors = new List<string>();
var json2 = "{\"myint\":3554860000,\"Mybool\":false}";
using (var reader = new FixedJsonTextReader(new StringReader(json2)))
{
var settings = new JsonSerializerSettings
{
Error = delegate(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs args)
{
Debug.WriteLine(args.ErrorContext.Error.Message);
errors.Add(args.ErrorContext.Error.Message);
args.ErrorContext.Handled = true;
}
};
var i = JsonSerializer.CreateDefault(settings).Deserialize<MyClass>(reader);
}
Assert.IsTrue(errors.Count <= 1); // Passes