Skip to content

Commit

Permalink
Merge branch 'release/2.1.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
dhenl2 committed Jan 31, 2024
2 parents 5a082d7 + edb9845 commit 506863d
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 29 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [2.1.4] - 2024-01-31
### CHANGED
- Changed the error message produced when more than 1 GraphQL error is encountered. Previously, all that was logged
was "More than 1 error encountered" which is too vague. This has been changed to "More than 1 GraphQLError
encountered" with additional logs detailing each GraphQL error encountered as "<message> @ locations/s <locations>...".

## [2.1.3] - 2024-01-31
### FIXED
- Added utility class `EntityCriteria` to package exports.
Expand Down Expand Up @@ -176,7 +182,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
### ADDED
- Alpha release

[Unreleased]: https://github.com/widesky/jswidesky-client/compare/master...2.1.1
[Unreleased]: https://github.com/widesky/jswidesky-client/compare/master...2.1.4
[1.0.0]: https://github.com/widesky/jswidesky-client/compare/1.0.0...1.0.0
[1.1.0]: https://github.com/widesky/jswidesky-client/compare/1.1.0...1.0.0
[1.1.1]: https://github.com/widesky/jswidesky-client/compare/1.1.1...1.1.0
Expand All @@ -194,3 +200,5 @@ This project adheres to [Semantic Versioning](http://semver.org/).
[2.1.0]: https://github.com/widesky/jswidesky-client/compare/2.1.0...2.0.6
[2.1.1]: https://github.com/widesky/jswidesky-client/compare/2.1.1...2.1.0
[2.1.2]: https://github.com/widesky/jswidesky-client/compare/2.1.2...2.1.1
[2.1.3]: https://github.com/widesky/jswidesky-client/compare/2.1.3...2.1.2
[2.1.4]: https://github.com/widesky/jswidesky-client/compare/2.1.4...2.1.3
27 changes: 21 additions & 6 deletions dist/wideskyClient.develop.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/wideskyClient.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@widesky/jswidesky-client",
"version": "2.1.3",
"version": "2.1.4",
"description": "WideSky Client in JavaScript",
"main": "index.js",
"private": false,
Expand All @@ -9,7 +9,7 @@
"coverage": "npx nyc --reporter=html --reporter=text mocha test/*/**.js",
"build:bundlejs:prd": "webpack --config webpack.config.js --mode production",
"build:bundlejs:dev": "webpack --config webpack.config.js --mode development",
"build": "run-s test build:bundlejs:prd build:bundlejs:dev"
"build": "npm install && run-s test build:bundlejs:prd build:bundlejs:dev"
},
"author": "WideSky.cloud <[email protected]>",
"license": "MIT",
Expand Down
6 changes: 5 additions & 1 deletion src/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const clientV2Functions = require("./functions/v2");
const { performOpInBatch, ...allBatchFunctions } = require("./functions/batch");
const cliProgress = require("cli-progress");
const bFormat = require("bunyan-format");
const {GraphQLError} = require("../errors");

let axios;
// Browser/Node axios import
Expand Down Expand Up @@ -423,11 +424,14 @@ class WideSkyClient {
return this._wsRawSubmit(method, uri, body, config);
}
else {
parsedErr = RequestError.make(err);
parsedErr = RequestError.make(err, this.logger);
}
}
}
this.logger.error(parsedErr);
if (parsedErr instanceof GraphQLError && parsedErr.errors.length > 1) {
parsedErr.errors.forEach((msg, i) => this.logger.error("GraphQL error #%d: %s", i + 1, msg));
}
throw parsedErr;
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@ class RequestError extends Error {
this.stack += reqError.stack.substring(reqError.stack.indexOf("\n"));
}

static make(reqError) {
static make(reqError, logger) {
if (lodash.has(reqError, "response.data")) {
const { data } = reqError.response;
if (lodash.has(data, "meta.dis")) {
return new HaystackError(data.meta.dis.substring(2), reqError);
}
else if (data.errors !== undefined && Array.isArray(data.errors) && data.errors.length > 0) {
let errMsg = "More than 1 error encountered";
let errMsg = "More than 1 GraphQLError encountered";
if (data.errors.length === 1) {
errMsg = data.errors[0].message;
}

logger.debug("Raw GraphQL error response: %j", data);
return new GraphQLError(errMsg.replace(/\n/g, ""), reqError);
}
else {
Expand Down Expand Up @@ -49,8 +50,13 @@ class HaystackError extends RequestError {
*/
class GraphQLError extends RequestError {
constructor(name, reqError) {
super(name, reqError);
this.errors = reqError.response.data.errors;
super(name.replace(/\n/g, ""), reqError);
this.errors = [];
for (const errorMsg of reqError.response.data.errors) {
const {message, locations} = errorMsg;
const locationsStr = locations.map((loc) => `line ${loc.line}:${loc.column}`);
this.errors.push(`${message.replace(/\n/g, " ")} @ location/s ${locationsStr.join(", ")}`);
}
}
}

Expand Down
29 changes: 20 additions & 9 deletions test/client/errors/RequestError.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
const {RequestError, HaystackError, GraphQLError} = require("../../../src/errors");
const { expect } = require("chai");
const {StubLogger} = require("../../stubs");

describe("RequestError", () => {
let logger = new StubLogger();
beforeEach(() => {
logger.resetHistory();
});

describe("make", () => {
it("should not create RequestError if no response received", () => {
const error = new Error("Test error");
const reqError = RequestError.make(error);
const reqError = RequestError.make(error, logger);
expect(reqError).to.be.instanceof(Error);
});

it("should not create RequestError if no data in response", () => {
// e.g. HTTP 403 errors don't typically have a message
const error = new Error("Test error");
error.response = {};
const reqError = RequestError.make(error);
const reqError = RequestError.make(error, logger);
expect(reqError).to.be.instanceof(Error);
});

Expand All @@ -24,7 +30,7 @@ describe("RequestError", () => {
nothingToSee: "here"
}
};
const reqError = RequestError.make(error);
const reqError = RequestError.make(error, logger);
expect(reqError).to.be.instanceof(Error);
});

Expand All @@ -38,7 +44,7 @@ describe("RequestError", () => {
}
}
};
const reqError = RequestError.make(error);
const reqError = RequestError.make(error, logger);
expect(reqError).to.be.instanceof(HaystackError);
expect(reqError.message).to.equal("HBadRequestError: It broken");
});
Expand All @@ -63,12 +69,14 @@ describe("RequestError", () => {
]
}
};
const reqError = RequestError.make(error);
const reqError = RequestError.make(error, logger);
expect(reqError).to.be.instanceof(GraphQLError);
expect(reqError.message).to.equal(
"Field \"search\" argument \"whereTag\" of type \"String!\" is required but not provided."
);
expect(reqError.errors).to.eql(error.response.data.errors);
expect(reqError.errors).to.eql([
"Field \"search\" argument \"whereTag\" of type \"String!\" is required but not provided. @ location/s line 5:5"
]);
});
});

Expand Down Expand Up @@ -99,10 +107,13 @@ describe("RequestError", () => {
]
}
};
const reqError = RequestError.make(error);
const reqError = RequestError.make(error, logger);
expect(reqError).to.be.instanceof(GraphQLError);
expect(reqError.message).to.equal("More than 1 error encountered");
expect(reqError.errors).to.eql(error.response.data.errors);
expect(reqError.message).to.equal("More than 1 GraphQLError encountered");
expect(reqError.errors).to.eql([
"Field \"search\" argument \"whereTag\" of type \"String!\" is required but not provided. @ location/s line 5:5",
"Field blah not blah blah @ location/s line 5:10"
]);
});
});
});
2 changes: 1 addition & 1 deletion test/client/functions/batch/deleteByFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe("client.batch.deleteByFilter", () => {
]
}
}
throw RequestError.make(fakeError);
throw RequestError.make(fakeError, log);
});

const { success, errors } = await ws.batch.deleteByFilter("bad filter");
Expand Down
2 changes: 1 addition & 1 deletion test/client/functions/batch/hisDeleteByFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ describe("client.batch.hisDeleteByFilter", () => {
]
}
}
throw RequestError.make(fakeError);
throw RequestError.make(fakeError, log);
});

const { success, errors } = await ws.batch.hisDeleteByFilter("bad filter", TIME_START, TIME_END);
Expand Down
2 changes: 1 addition & 1 deletion test/client/functions/batch/hisReadByFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe("client.batch.hisReadByFilter", () => {
]
}
}
throw RequestError.make(fakeError);
throw RequestError.make(fakeError, log);
});


Expand Down
5 changes: 5 additions & 0 deletions test/stubs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ const StubLogger = function() {
['fatal','error','warn','info','debug','trace'].forEach((l) => {
self[l] = sinon.stub();
});
self.resetHistory = () => {
['fatal','error','warn','info','debug','trace'].forEach((l) => {
self[l].resetHistory();
});
}
};

/**
Expand Down

0 comments on commit 506863d

Please sign in to comment.