Repository: aws/aws-sdk-js
Pull request #4221 (2022-09-28 19:31:14)

AWSQuery compatible error code
* Reverts previous change https://github.com/aws/aws-sdk-js/pull/4105 that used a different approach
* Uses the header value to return AWSQuery compatible error code.

##### Checklist

- [ x] `npm run test` passes
- [ x] changelog is added, `npm run add-change`



Patch:
diff --git a/.changes/next-release/feature-AwsQueryCompatible-c54d1937.json b/.changes/next-release/feature-AwsQueryCompatible-c54d1937.json
new file mode 100644
index 000000000..5ea260f80
--- /dev/null
+++ b/.changes/next-release/feature-AwsQueryCompatible-c54d1937.json
@@ -0,0 +1,5 @@
+{
+  "type": "feature",
+  "category": "AwsQueryCompatible",
+  "description": "Return  AWSQuery compatible error code from HTTP header if found"
+}
\ No newline at end of file
diff --git a/lib/event_listeners.js b/lib/event_listeners.js
index 0002c59c6..ac53ecd2e 100644
--- a/lib/event_listeners.js
+++ b/lib/event_listeners.js
@@ -319,12 +319,10 @@ AWS.EventListeners = {
     });
 
     add('ERROR', 'error', function ERROR(err, resp) {
-      var errorCodeMapping = resp.request.service.api.errorCodeMapping;
-      if (errorCodeMapping && err && err.code) {
-        var mapping = errorCodeMapping[err.code];
-        if (mapping) {
-          resp.error.code = mapping.code;
-        }
+      var headers = resp.httpResponse.headers;
+      var queryErrorCode = headers ? headers['x-amzn-query-error'] : undefined;
+      if (queryErrorCode) {
+        resp.error.code = queryErrorCode.split(';')[0];
       }
     }, true);
 
diff --git a/lib/model/api.js b/lib/model/api.js
index 33727e32f..d21ba4bbd 100644
--- a/lib/model/api.js
+++ b/lib/model/api.js
@@ -80,7 +80,6 @@ function Api(api, options) {
     property(this, 'documentation', api.documentation);
     property(this, 'documentationUrl', api.documentationUrl);
   }
-  property(this, 'errorCodeMapping', api.awsQueryCompatible);
 }
 
 /**


Test patch:
diff --git a/test/helpers.js b/test/helpers.js
index 6dcf28806..935855d8d 100644
--- a/test/helpers.js
+++ b/test/helpers.js
@@ -146,7 +146,7 @@
     return expect(results[0]).to.eql(results[1]);
   };
 
-  let mockServiceParams = {
+  MockService = AWS.Service.defineService('mock', {
     serviceIdentifier: 'mock',
     initialize: function(config) {
       AWS.Service.prototype.initialize.call(this, config);
@@ -166,11 +166,7 @@
           message: null
         };
       });
-    }
-  };
-
-  MockService = AWS.Service.defineService('mock', {
-    ...mockServiceParams,
+    },
     api: new AWS.Model.Api({
       metadata: {
         endpointPrefix: 'mockservice',
@@ -186,7 +182,15 @@
       customApi.metadata.signatureVersion = 'v4';
     }
     return AWS.Service.defineService('mock', {
-      ...mockServiceParams,
+      serviceIdentifier: 'mock',
+      initialize: function(config) {
+        AWS.Service.prototype.initialize.call(this, config);
+        this.config.credentials = {
+          accessKeyId: 'akid',
+          secretAccessKey: 'secret'
+        };
+        this.config.region = this.config.region || 'mock-region';
+      },
       api: new AWS.Model.Api(customApi)
     });
   };
diff --git a/test/service.spec.js b/test/service.spec.js
index 80624c6dc..a43c036f9 100644
--- a/test/service.spec.js
+++ b/test/service.spec.js
@@ -1,50 +1,36 @@
 // Generated by CoffeeScript 1.12.3
 (function() {
-  var AWS, MockService, helpers, metadata, MockServiceFromApi, hasProp = {}.hasOwnProperty;
+  var AWS, MockService, helpers, metadata,
+    hasProp = {}.hasOwnProperty;
 
   helpers = require('./helpers');
 
   AWS = helpers.AWS;
 
   MockService = helpers.MockService;
-  MockServiceFromApi = helpers.MockServiceFromApi;
 
   metadata = require('../apis/metadata.json');
 
   describe('AWSQuery compatible error codes', function () {
     let service;
-    let mapping = { ServiceError: { code: 'Custom.ErrorCode' } };
-    let api = {
-      metadata: {
-        endpointPrefix: 'mockservice',
-        signatureVersion: 'v4'
-      },
-      awsQueryCompatible: {
-        ...mapping
-      }
-    };
 
     beforeEach(function(done) {
-      let CustomService = MockServiceFromApi(api);
-      service = new CustomService({maxRetries: 0});
+      service = new MockService({maxRetries: 0});
       return done();
     });
 
-    it('awsQueryCompatible trait is translated to correct property', function() {
-      expect(service.api.errorCodeMapping).toBeDefined;
-      return expect(service.api.errorCodeMapping.ServiceError.code).to.equal(mapping.ServiceError.code);
-    });
-
-    it('can receive a modeled exception with error code mapping', function () {
-      helpers.mockHttpResponse(500, {}, ['ServiceError']);
+    it('can receive awsquery compatible error code when header present', function () {
+      helpers.mockHttpResponse(500, {
+        'x-amzn-query-error': 'AwsQueryError;Sender'
+      }, ['ServiceError']);
       return (service.makeRequest('operation', {}, function (err, data) {
-        expect(err.code).to.equal(mapping.ServiceError.code);
+        expect(err.code).to.equal('AwsQueryError');
         expect(err.statusCode).to.equal(500);
         return expect(data).to.equal(null);
       }));
     });
 
-    it('can receive a modeled exception without error code mapping', function () {
+    it('can receive error code when header not present', function () {
       helpers.mockHttpResponse(500, {}, ['ServiceUnavailableException']);
       return (service.makeRequest('operation', {}, function (err, data) {
         expect(err.code).to.equal('ServiceUnavailableException');
@@ -52,27 +38,16 @@
         return expect(data).to.equal(null);
       }));
     });
-  });
-
-  describe('AWSQuery compatible error codes without mapping', function () {
-    let service;
-
-    beforeEach(function (done) {
-      service = new MockService({ maxRetries: 0 });
-      return done();
-    });
-
-    it('error code mapping property is undefined', function () {
-      return expect(service.api.errorCodeMapping).toBeUndefined;
-    });
 
-    it('can receive a modeled exception', function () {
-      helpers.mockHttpResponse(500, {}, ['ServiceError']);
-      return service.makeRequest('operation', {}, function (err, data) {
+    it('can receive awsquery compatible error code when header empty', function () {
+      helpers.mockHttpResponse(500, {
+        'x-amzn-query-error': ''
+      }, ['ServiceError']);
+      return (service.makeRequest('operation', {}, function (err, data) {
         expect(err.code).to.equal('ServiceError');
         expect(err.statusCode).to.equal(500);
         return expect(data).to.equal(null);
-      });
+      }));
     });
   });