Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
What is the appropriate error code to return?
Message
 
 
To
All
General information
Forum:
ASP.NET
Category:
Other
Title:
What is the appropriate error code to return?
Environment versions
Environment:
C# 5.0
OS:
Windows 10
Database:
MS SQL Server
Miscellaneous
Thread ID:
01639317
Message ID:
01639317
Views:
56
UPDATE. Got it working with Unauthorized code.

Hi everybody,

I'm trying to create an error condition in my API controller. Here is my current code
 if (_operatorAdapter.CanEditOperator(oper, OperatorSession))
            {
                EditOperatorViewModel editOperatorViewModel = _editOperatorViewModelHelper.LoadEditOperatorViewModel(oper, OperatorSession.OpCode, OperatorSession.Rights);

                // Contact
                editOperatorViewModel.ContactPerson = _contactsAdapter.GetContactsAndChildrenById(editOperatorViewModel.ContactId);

                return Ok(editOperatorViewModel);
            }
            else
            {
                var response = Messages.cannotEditOperator;
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError )
                {
                    Content = new StringContent(response),
                    ReasonPhrase = response
                });
            }
and I am also trying to handle this error in the controller's code.

I've studied this link

https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

but still not sure what is the best code to return.

My problem is that we have the following in the app.js file:
app.factory('badRequestInterceptor', ['$rootScope', '$q', function ($rootScope, $q) {
        return {
            'responseError': function (rejection) {
                if (rejection.status === 400) {
                    $rootScope.$broadcast("sm:badRequest", rejection.data);
                }
                return $q.reject(rejection);
            }
        };
    }]);

    app.factory('noConnectionInterceptor', ['$rootScope', '$q', function ($rootScope, $q) {
        return {            
            'responseError': function (rejection) {               
                if (rejection.status === -1) {
                    $rootScope.$broadcast("sm:noConnection");
                }
                return $q.reject(rejection);
            }
        };
    }]);
    
    app.factory('internalServerErrorInterceptor', ['$rootScope', '$q', function ($rootScope, $q) {
        return {
            'responseError': function (rejection) {
                if (rejection.status === 500) {
                    var errors = {
                        message: 'An error has occurred. Please try again or contact system support.'
                    };
                    $rootScope.$broadcast("sm:serverError", errors);
                }
                
                return $q.reject(rejection);
            }
        };
and therefore the error is getting intercepted and it's not the response I want. I want to handle that error myself.

So, the question is - what code should I use?

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Reply
Map
View

Click here to load this message in the networking platform