comparison Core/HttpServer/MongooseServer.cpp @ 1569:27774f6f84e4

improved error handling in http answers
author jodogne
date Sun, 23 Aug 2015 11:01:15 +0200
parents 818ae9bc493a
children 2bd2c280f9b5
comparison
equal deleted inserted replaced
1568:818ae9bc493a 1569:27774f6f84e4
717 } 717 }
718 718
719 719
720 LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri); 720 LOG(INFO) << EnumerationToString(method) << " " << Toolbox::FlattenUri(uri);
721 721
722 bool found = false;
723 722
724 try 723 try
725 { 724 {
726 if (that->HasHandler()) 725 bool found = false;
727 { 726
728 found = that->GetHandler().Handle(output, method, uri, headers, argumentsGET, body.c_str(), body.size()); 727 try
729 } 728 {
729 if (that->HasHandler())
730 {
731 found = that->GetHandler().Handle(output, method, uri, headers, argumentsGET, body.c_str(), body.size());
732 }
733 }
734 catch (boost::bad_lexical_cast&)
735 {
736 throw OrthancException(ErrorCode_BadParameterType);
737 }
738 catch (std::runtime_error&)
739 {
740 // Presumably an error while parsing the JSON body
741 throw OrthancException(ErrorCode_BadRequest);
742 }
743
744 if (!found)
745 {
746 throw OrthancException(ErrorCode_UnknownResource);
747 }
730 } 748 }
731 catch (OrthancException& e) 749 catch (OrthancException& e)
732 { 750 {
733 // Using this candidate handler results in an exception 751 // Using this candidate handler results in an exception
734 LOG(ERROR) << "Exception in the HTTP handler: " << e.What(); 752 LOG(ERROR) << "Exception in the HTTP handler: " << e.What();
735 753
754 HttpStatus status;
755
736 try 756 try
737 { 757 {
738 switch (e.GetErrorCode()) 758 switch (e.GetErrorCode())
739 { 759 {
740 case ErrorCode_InexistentFile: 760 case ErrorCode_InexistentFile:
741 case ErrorCode_InexistentItem: 761 case ErrorCode_InexistentItem:
742 case ErrorCode_UnknownResource: 762 case ErrorCode_UnknownResource:
743 output.SendStatus(HttpStatus_404_NotFound); 763 status = HttpStatus_404_NotFound;
764 break;
765
766 case ErrorCode_BadRequest:
767 case ErrorCode_UriSyntax:
768 case ErrorCode_BadParameterType:
769 status = HttpStatus_400_BadRequest;
744 break; 770 break;
745 771
746 case ErrorCode_BadRequest:
747 case ErrorCode_UriSyntax:
748 output.SendStatus(HttpStatus_400_BadRequest);
749 break;
750
751 default: 772 default:
752 { 773 status = HttpStatus_500_InternalServerError;
753 Json::Value message = Json::objectValue; 774 break;
754 message["ErrorCode"] = e.GetErrorCode();
755 message["Description"] = e.GetDescription(e.GetErrorCode());
756 message["Message"] = e.What();
757 std::string s = message.toStyledString();
758 output.SendStatus(HttpStatus_500_InternalServerError, s);
759 }
760 } 775 }
776
777 Json::Value message = Json::objectValue;
778 message["HttpStatus"] = status;
779 message["HttpError"] = EnumerationToString(status);
780 message["OrthancStatus"] = e.GetErrorCode();
781 message["OrthancError"] = e.GetDescription(e.GetErrorCode());
782 message["Message"] = e.What();
783 message["Uri"] = request->uri;
784 message["Method"] = EnumerationToString(method);
785 std::string s = message.toStyledString();
786 output.SendStatus(status, s);
761 } 787 }
762 catch (OrthancException&) 788 catch (OrthancException&)
763 { 789 {
764 // An exception here reflects the fact that an exception was 790 // An exception here reflects the fact that the status code
765 // triggered after the status code was sent by the HTTP handler. 791 // was already set by the HTTP handler.
766 } 792 }
767 793
768 return; 794 return;
769 }
770 catch (boost::bad_lexical_cast&)
771 {
772 LOG(ERROR) << "Exception in the HTTP handler: Bad lexical cast";
773 output.SendStatus(HttpStatus_400_BadRequest, "Cannot cast some argument");
774 return;
775 }
776 catch (std::runtime_error&)
777 {
778 LOG(ERROR) << "Exception in the HTTP handler: Presumably a bad JSON request";
779 output.SendStatus(HttpStatus_400_BadRequest, "Bad JSON request");
780 return;
781 }
782
783 if (!found)
784 {
785 output.SendStatus(HttpStatus_404_NotFound);
786 } 795 }
787 } 796 }
788 797
789 798
790 #if MONGOOSE_USE_CALLBACKS == 0 799 #if MONGOOSE_USE_CALLBACKS == 0