Przeglądaj źródła

优化jsonrpc2.ParseResponse()函数

niujiuru 2 tygodni temu
rodzic
commit
52b7e0186a
1 zmienionych plików z 8 dodań i 6 usunięć
  1. 8 6
      utils/jsonrpc2/rpc.go

+ 8 - 6
utils/jsonrpc2/rpc.go

@@ -93,12 +93,6 @@ func ParseResponse(input any) (*Response, error) {
 		return nil, fmt.Errorf("unmarshal response: %w", err)
 	}
 
-	if resp.ID == nil {
-		if resp.Error == nil || resp.Error.Code != ErrParse {
-			return nil, errors.New(`response must contain an "id" field`)
-		}
-	}
-
 	if resp.Error == nil && resp.Result == nil {
 		return nil, errors.New(`response must contain either "result" or "error" field`)
 	}
@@ -107,6 +101,14 @@ func ParseResponse(input any) (*Response, error) {
 		return nil, errors.New(`response can't contain both "result" and "error" field`)
 	}
 
+	if resp.Error != nil && resp.ID == nil && resp.Error.Code != ErrParse {
+		return nil, errors.New(`"error" must contain an "id" field`)
+	}
+
+	if resp.Result != nil && resp.ID == nil {
+		return nil, errors.New(`"result" must contain an "id" field`)
+	}
+
 	return &resp, nil
 }