Not entirely sure if this is the reason or not but it's my only theory at the moment. Currently my WebAPI tries to send a response for any GET
calls that come through to the controller. CORS is enabled in the main WebAPIConfig and yet any time the response contains another class, I get an InternalServerError 500
. It fails with a 500 If the Rule has an Element/View included with the data being sent - otherwise, goes through fine.
// Server Side
public class RuleController : ApiController
{
private readonly IRepository<Rule> _repo;
public RuleController()
{
var mock = new Mock<IRepository<Rule>>();
mock.Setup(m => m.GetAll()).Returns(new List<Rule>
{
new Rule
{
Id = 2,
Name = "TestRule819416",
// If either of these elements are uncommmented, a 500 error is returned, otherwise it comes back fine.
//Element = new Element {
// Id = 9461313,
// Name = "safjwofmfs"
//},
//View = new View
//{
// Id = 3269591,
// Name = "sofijhopfjsapof"
//}
},
}.AsQueryable());
_repo = mock.Object;
}
[ResponseType(typeof(Rule))]
public IHttpActionResult GetAll()
{
var elements = _repo.GetAll();
if (elements == null)
return NotFound();
return Ok(elements);
}
}
Could this potentially be an error on the client's side?
// Client Side
//
import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { Rule } from '../domain/Rule';
@Injectable()
export class ErrorService {
constructor(private http: Http) {
}
private rulesUrl = 'api/rule';
getRules(): Promise<Rule[]> {
return this.http.get(this.rulesUrl)
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('An error occured attempt to retrieve from server', error);
return Promise.reject(error.message || error);
}
private extractData(response: Response): Rule[] {
let data = response.json() as Rule[];
return data;
}
}