0

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;
    }
}
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
prestonsmith
  • 758
  • 1
  • 9
  • 29
  • 1
    A 500 indicates an unexpected failure on the server side, right? And usually the way to find the cause is to check the server-side logs—e.g., in `%SystemDrive%\inetpub\logs\LogFiles` and/or `%SystemDrive%\Windows\System32\LogFiles\HTTPERR`. See also http://stackoverflow.com/questions/6426375/where-can-i-find-the-iis-logs/6426399#6426399 – sideshowbarker Mar 07 '17 at 03:55
  • Shouldn't the IRepository _repo be Mock> _repo; – Marcus Höglund Mar 07 '17 at 06:42
  • 1
    As @sideshowbarker said, definitely not a client side issue. If you debug, where does the error throw? If you debug, what is the actual exception message? – MichaelDotKnox Mar 07 '17 at 12:45
  • @MarcusH I don't think that's the issue, that's just regular DIb+ Mocking. – prestonsmith Mar 07 '17 at 12:47
  • @sideshowbarker & MichaelDotKnox yeah, I was pretty sure that it was server side as well but just thought maybe it might have something to do with the request? The error is thrown somewhere in .NET code after the OK response is returned - hence why I'm not able to debug it well. I'll check those logs in the meantime as you've suggested sideshowbarker - thanks for that! – prestonsmith Mar 07 '17 at 12:52

1 Answers1

0

As both @MichaelDotKnox and @sideshowbarker mentioned, I needed to check internal logs to figure out why the server wasn't sending over the information.

To summarize their answers, check logs in these locations about what IIS is doing: Where can I find the IIS logs?

%SystemDrive%\inetpub\logs\LogFiles and/or

%SystemDrive%\Windows\System32\LogFiles\HTTPERR

and then step through your code to see where the exceptions they might occur.

In my case, my classes were suffering from an InvalidCastException that was getting swallowed up in all the mess. Once I found and resolved it, my sweet, sweet data started getting sent over without fail.

Community
  • 1
  • 1
prestonsmith
  • 758
  • 1
  • 9
  • 29