comparison Core/HttpServer/MongooseServer.cpp @ 1575:f8aae45011c9

exceptions now carry HTTP status
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 25 Aug 2015 14:00:57 +0200
parents 3232f1c995a5
children bd1889029cbb
comparison
equal deleted inserted replaced
1574:0c29ebe80ac9 1575:f8aae45011c9
732 method, uri, headers, argumentsGET, body.c_str(), body.size()); 732 method, uri, headers, argumentsGET, body.c_str(), body.size());
733 } 733 }
734 } 734 }
735 catch (boost::bad_lexical_cast&) 735 catch (boost::bad_lexical_cast&)
736 { 736 {
737 throw OrthancException(ErrorCode_BadParameterType); 737 throw OrthancException(ErrorCode_BadParameterType, HttpStatus_400_BadRequest);
738 } 738 }
739 catch (std::runtime_error&) 739 catch (std::runtime_error&)
740 { 740 {
741 // Presumably an error while parsing the JSON body 741 // Presumably an error while parsing the JSON body
742 throw OrthancException(ErrorCode_BadRequest); 742 throw OrthancException(ErrorCode_BadRequest, HttpStatus_400_BadRequest);
743 } 743 }
744 744
745 if (!found) 745 if (!found)
746 { 746 {
747 throw OrthancException(ErrorCode_UnknownResource); 747 throw OrthancException(ErrorCode_UnknownResource);
750 catch (OrthancException& e) 750 catch (OrthancException& e)
751 { 751 {
752 // Using this candidate handler results in an exception 752 // Using this candidate handler results in an exception
753 LOG(ERROR) << "Exception in the HTTP handler: " << e.What(); 753 LOG(ERROR) << "Exception in the HTTP handler: " << e.What();
754 754
755 HttpStatus status; 755 Json::Value message = Json::objectValue;
756 message["HttpError"] = EnumerationToString(e.GetHttpStatus());
757 message["HttpStatus"] = e.GetHttpStatus();
758 message["Message"] = e.What();
759 message["Method"] = EnumerationToString(method);
760 message["OrthancError"] = EnumerationToString(e.GetErrorCode());
761 message["OrthancStatus"] = e.GetErrorCode();
762 message["Uri"] = request->uri;
763
764 std::string info = message.toStyledString();
756 765
757 try 766 try
758 { 767 {
759 switch (e.GetErrorCode()) 768 output.SendStatus(e.GetHttpStatus(), info);
760 {
761 case ErrorCode_InexistentFile:
762 case ErrorCode_InexistentItem:
763 case ErrorCode_UnknownResource:
764 status = HttpStatus_404_NotFound;
765 break;
766
767 case ErrorCode_BadRequest:
768 case ErrorCode_UriSyntax:
769 case ErrorCode_BadParameterType:
770 status = HttpStatus_400_BadRequest;
771 break;
772
773 default:
774 status = HttpStatus_500_InternalServerError;
775 break;
776 }
777
778 Json::Value message = Json::objectValue;
779 message["HttpStatus"] = status;
780 message["HttpError"] = EnumerationToString(status);
781 message["OrthancStatus"] = e.GetErrorCode();
782 message["OrthancError"] = e.GetDescription(e.GetErrorCode());
783 message["Message"] = e.What();
784 message["Uri"] = request->uri;
785 message["Method"] = EnumerationToString(method);
786 std::string s = message.toStyledString();
787 output.SendStatus(status, s);
788 } 769 }
789 catch (OrthancException&) 770 catch (OrthancException&)
790 { 771 {
791 // An exception here reflects the fact that the status code 772 // An exception here reflects the fact that the status code
792 // was already set by the HTTP handler. 773 // was already set by the HTTP handler.