Khaled A
Khaled A

Reputation: 11

Multiple exception status codes with status text as "ok"

I'm working on an exception handler in a small project, no matter what is the exception or the exception code the HTTP response always returns a status text of "ok"

Console log

.net core 5 Controller class :

using API.Data;
using API.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{
    public class BuggyController : BaseApiController
    {
        private readonly ApplicationDbContext _context;
        public BuggyController(ApplicationDbContext context)
        {
            _context = context;
        }

        [Authorize]
        [HttpGet("auth")]
        public ActionResult<string> GetSecret()
        {
            return "secret test";
        }

        [HttpGet("not-found")]
        public ActionResult<AppUser> GetNotFound()
        {
            var thing = _context.Users.Find(-1);

            if (thing == null)
                return NotFound();

            else
                return thing;
        }

        [HttpGet("server-error")]
        public ActionResult<string> GetServerError()
        {
            var thing = _context.Users.Find(-1);

            var thingToReturn = thing.ToString();

            return thingToReturn;
        }

        [HttpGet("bad-request")]
        public ActionResult<string> GetBadRequest()
        {
            return BadRequest("This was not a good request");
        }
    }
}

The angular component to test the exceptions:

import { HttpClient } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-test-errors',
  templateUrl: './test-errors.component.html',
  styleUrls: ['./test-errors.component.css']
})
export class TestErrorsComponent implements OnInit {
  baseUrl = "https://localhost:5001/api/";

  constructor(private http: HttpClient) { }

  ngOnInit(): void {
  }

  get404Error() {
    this.http.get(this.baseUrl + 'buggy/not-found').subscribe(response => {

      console.log(response)


    }, error => {
      console.log(error);
    })  

  }

  get400ValidationError() {
    this.http.post(this.baseUrl + 'buggy/register', {}).subscribe(response => {

      console.log(response)


    }, error => {
      console.log(error);
    })
  }


  get400Error() {
    this.http.get(this.baseUrl + 'buggy/bad-request').subscribe(response => {

      console.log(response)


    }, error => {
      console.log(error);
    })

  }

  get500Error() {
    this.http.get(this.baseUrl + 'buggy/server-error').subscribe(response => {

      console.log(response)


    }, error => {
      console.log(error);
    })

  }

  get401Error() {
    this.http.get(this.baseUrl + 'buggy/auth').subscribe(response => {

      console.log(response)


    }, error => {
      console.log(error);
    })

  }
}

I would very much appreciate some assistance on why am i getting the status text as ok. I double and triple checked everything and its still the same. Thank you in advnace

Upvotes: 0

Views: 1159

Answers (1)

Khaled A
Khaled A

Reputation: 11

I found the answer on the course's QA in Udemy. It has to do with http/2 and default values in angular client itself. In short in http/2 requests the statusText is removed from the response and angular fills in the missing info with default data ( being statusText=Ok ). It's also a problem cited elsewhere

Course link main reference

Upvotes: 1

Related Questions