From 7f20ad32b6d6b0c376d5b4d66d3048fc982994d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Fri, 12 Jan 2024 11:51:06 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E7=94=A8API=E6=97=B6=E6=89=BE?= =?UTF-8?q?=E4=B8=8D=E5=88=B0=E6=9C=8D=E5=8A=A1=E6=88=96=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=97=B6=E4=B9=9F=E6=8F=90=E7=A4=BAJSON=EF=BC=8C=E9=98=B2?= =?UTF-8?q?=E6=AD=A2=E5=B0=8F=E7=99=BD=E5=BC=80=E5=8F=91=E8=80=85=E4=B8=8D?= =?UTF-8?q?=E7=9F=A5=E9=81=93=E5=A6=82=E4=BD=95=E8=8E=B7=E5=8F=96=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/rest_server.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/internal/nodes/rest_server.go b/internal/nodes/rest_server.go index f4f4868d..38a80917 100644 --- a/internal/nodes/rest_server.go +++ b/internal/nodes/rest_server.go @@ -67,6 +67,11 @@ func (this *RestServer) handle(writer http.ResponseWriter, req *http.Request) { var matches = servicePathReg.FindStringSubmatch(path) if len(matches) != 3 { writer.WriteHeader(http.StatusNotFound) + this.writeJSON(writer, maps.Map{ + "code": "404", + "message": "invalid api path '" + path + "'", + "data": maps.Map{}, + }, shouldPretty) return } @@ -76,11 +81,21 @@ func (this *RestServer) handle(writer http.ResponseWriter, req *http.Request) { serviceType, ok := restServicesMap[serviceName] if !ok { writer.WriteHeader(http.StatusNotFound) + this.writeJSON(writer, maps.Map{ + "code": "404", + "message": "service '" + serviceName + "' not found", + "data": maps.Map{}, + }, shouldPretty) return } if len(methodName) == 0 { writer.WriteHeader(http.StatusNotFound) + this.writeJSON(writer, maps.Map{ + "code": "404", + "message": "method '" + methodName + "' not found", + "data": maps.Map{}, + }, shouldPretty) return } @@ -94,19 +109,39 @@ func (this *RestServer) handle(writer http.ResponseWriter, req *http.Request) { method = serviceType.MethodByName(methodName) if !method.IsValid() { writer.WriteHeader(http.StatusNotFound) + this.writeJSON(writer, maps.Map{ + "code": "404", + "message": "method '" + methodName + "' not found", + "data": maps.Map{}, + }, shouldPretty) return } } else { writer.WriteHeader(http.StatusNotFound) + this.writeJSON(writer, maps.Map{ + "code": "404", + "message": "method '" + methodName + "' not found", + "data": maps.Map{}, + }, shouldPretty) return } } if method.Type().NumIn() != 2 || method.Type().NumOut() != 2 { writer.WriteHeader(http.StatusNotFound) + this.writeJSON(writer, maps.Map{ + "code": "404", + "message": "method '" + methodName + "' not found", + "data": maps.Map{}, + }, shouldPretty) return } if method.Type().In(0).Name() != "Context" { writer.WriteHeader(http.StatusNotFound) + this.writeJSON(writer, maps.Map{ + "code": "404", + "message": "method '" + methodName + "' not found (or invalid context)", + "data": maps.Map{}, + }, shouldPretty) return }