Add additionalInfo to CloudError (#63)

* Add additionalInfo to CloudError

* Change mapper from Object to Composite

* Add additional info interface
This commit is contained in:
Kamil Pajdzik 2018-09-19 18:46:22 -07:00 коммит произвёл GitHub
Родитель eb2d3b9677
Коммит 059b674068
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 78 добавлений и 4 удалений

Просмотреть файл

@ -28,6 +28,26 @@ export interface CloudError extends Error {
* @property {any} [innerError] The inner error parsed from the body of the http error response
*/
innerError?: any;
/**
* @property {AdditionalInfoElement} [innerError] The additional error information
*/
additionalInfo?: AdditionalInfoElement;
}
/**
* @interface
* Additional data for an instance of CloudError.
*/
export interface AdditionalInfoElement {
/**
* @property {string} [type] Type of the data.
*/
type?: string;
/**
* @property {string} [info] Additional info.
*/
info?: string;
}
export const CloudErrorMapper: CompositeMapper = {
@ -75,7 +95,31 @@ export const CloudErrorMapper: CompositeMapper = {
type: {
name: "Object"
}
},
additionalInfo: {
required: false,
serializedName: "additionalInfo",
type: {
name: "Composite",
className: "AdditionalInfoElement",
modelProperties: {
type: {
required: true,
serializedName: "type",
type: {
name: "String"
}
},
info: {
required: false,
serializedName: "info",
type: {
name: "Object"
}
}
}
}
}
}
}
};
};

Просмотреть файл

@ -4,7 +4,7 @@ import { expect } from "chai";
describe("CloudError", () => {
const serializer = new Serializer({"CloudError": CloudErrorMapper});
const serializer = new Serializer({ "CloudError": CloudErrorMapper });
describe("serialization", () => {
it("serializes properly required properties", () => {
@ -30,7 +30,7 @@ describe("CloudError", () => {
code: "200",
message: "test message",
target: "my target",
details: [ { name: "error", code: "404", message: "not found" }, { name: "error", code: "500", message: "Internal error" } ],
details: [{ name: "error", code: "404", message: "not found" }, { name: "error", code: "500", message: "Internal error" }],
innerError: new Error("Resource not found"),
stack: "call stack"
};
@ -73,7 +73,7 @@ describe("CloudError", () => {
code: "200",
message: "test message",
target: "my target",
details: [ { code: "404", message: "not found" }, { code: "500", message: "Internal error" } ],
details: [{ code: "404", message: "not found" }, { code: "500", message: "Internal error" }],
innererror: new Error("Resource not found"),
stack: "call stack"
};
@ -85,5 +85,35 @@ describe("CloudError", () => {
expect(deserializedCloudError.innerError).to.be.equal(cloudError.innererror);
expect(deserializedCloudError.stack).to.not.exist;
});
it("should correctly deserialize additionalInfo", function (done) {
const errorBody = {
"code": "BadArgument",
"message": "The provided database foo has an invalid username.",
"target": "query",
"details": [{
"code": "301",
"target": "$search",
"message": "$search query option not supported",
"additionalInfo": {
"type": "SomeErrorType",
"info": {
"someProperty": "SomeValue"
}
}
}]
};
const deserializedError = serializer.deserialize(CloudErrorMapper, errorBody, "deserializedCloudError");
expect(deserializedError.code).to.equal("BadArgument");
expect(deserializedError.message).to.equal("The provided database foo has an invalid username.");
expect(deserializedError.target).to.equal("query");
expect(deserializedError.details.length).to.equal(1);
expect(deserializedError.details![0].code).to.equal("301");
expect(deserializedError.details![0].additionalInfo.type).to.equal("SomeErrorType");
expect(deserializedError.details![0].additionalInfo.info.someProperty).to.equal("SomeValue");
done();
});
});
});