async modifyResponse()

in alibabacloud-gateway-oss/ts/src/client.ts [231:328]


  async modifyResponse(context: $SPI.InterceptorContext, attributeMap: $SPI.AttributeMap): Promise<void> {
    let request = context.request;
    let response = context.response;
    let bodyStr : string = null;
    if (Util.is4xx(response.statusCode) || Util.is5xx(response.statusCode)) {
      bodyStr = await Util.readAsString(response.body);
      if (!Util.empty(bodyStr)) {
        let respMap : {[key: string ]: any} = XML.parseXml(bodyStr, null);
        let err : {[key: string ]: any} = Util.assertAsMap(respMap["Error"]);
        throw $tea.newError({
          code: err["Code"],
          message: err["Message"],
          data: {
            statusCode: response.statusCode,
            requestId: err["RequestId"],
            ecCode: err["EC"],
            Recommend: err["RecommendDoc"],
            hostId: err["HostId"],
            AccessDeniedDetail: err["AccessDeniedDetail"],
          },
        });
      } else {
        let headers : {[key: string ]: string} = response.headers;
        let requestId = headers["x-oss-request-id"];
        let ecCode = headers["x-oss-ec-code"];
        throw $tea.newError({
          code: response.statusCode,
          message: null,
          data: {
            statusCode: response.statusCode,
            requestId: `${requestId}`,
            ecCode: ecCode,
          },
        });
      }

    }

    let ctx : {[key: string ]: string} = attributeMap.key;
    if (!Util.isUnset(ctx)) {
      if (!Util.isUnset(ctx["crc"]) && !Util.isUnset(response.headers["x-oss-hash-crc64ecma"]) && !String.equals(ctx["crc"], response.headers["x-oss-hash-crc64ecma"])) {
        throw $tea.newError({
          code: "CrcNotMatched",
          data: {
            clientCrc: ctx["crc"],
            serverCrc: response.headers["x-oss-hash-crc64ecma"],
          },
        });
      }

      if (!Util.isUnset(ctx["md5"]) && !Util.isUnset(response.headers["content-md5"]) && !String.equals(ctx["md5"], response.headers["content-md5"])) {
        throw $tea.newError({
          code: "MD5NotMatched",
          data: {
            clientMD5: ctx["md5"],
            serverMD5: response.headers["content-md5"],
          },
        });
      }

    }

    if (!Util.isUnset(response.body)) {
      if (Util.equalNumber(response.statusCode, 204)) {
        await Util.readAsString(response.body);
      } else if (String.equals(request.bodyType, "xml")) {
        bodyStr = await Util.readAsString(response.body);
        response.deserializedBody = bodyStr;
        if (!Util.empty(bodyStr)) {
          // var result : any = OSS_Util.parseXml(bodyStr, request.action);
          let result : any = XML.parseXml(bodyStr, null);
          try {
            response.deserializedBody = Util.assertAsMap(result);
          } catch (error) {
            response.deserializedBody = result;
          }          
        }

      } else if (Util.equalString(request.bodyType, "binary")) {
        response.deserializedBody = response.body;
      } else if (Util.equalString(request.bodyType, "byte")) {
        let byt = await Util.readAsBytes(response.body);
        response.deserializedBody = byt;
      } else if (Util.equalString(request.bodyType, "string")) {
        response.deserializedBody = await Util.readAsString(response.body);
      } else if (Util.equalString(request.bodyType, "json")) {
        let obj = await Util.readAsJSON(response.body);
        let res = Util.assertAsMap(obj);
        response.deserializedBody = res;
      } else if (Util.equalString(request.bodyType, "array")) {
        response.deserializedBody = await Util.readAsJSON(response.body);
      } else {
        response.deserializedBody = await Util.readAsString(response.body);
      }

    }

  }