Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/abstractions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/kiota-abstractions",
"version": "1.0.0-preview.16",
"version": "1.0.0-preview.17",
"description": "Core abstractions for kiota generated libraries in TypeScript and JavaScript",
"main": "dist/cjs/src/index.js",
"module": "dist/es/src/index.js",
Expand Down
6 changes: 6 additions & 0 deletions packages/abstractions/src/apiError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ export class ApiError extends Error {
public constructor(message?: string) {
super(message);
}

/** The status code for the error. */
public responseStatusCode?: number;

/** The Response Headers. */
public responseHeaders: Record<string, string[]> = {};
}
2 changes: 1 addition & 1 deletion packages/authentication/azure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@azure/core-auth": "^1.3.2",
"@microsoft/kiota-abstractions": "1.0.0-preview.16",
"@microsoft/kiota-abstractions": "1.0.0-preview.17",
"@opentelemetry/api": "^1.2.0",
"tslib": "^2.3.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/spfx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@microsoft/kiota-abstractions": "1.0.0-preview.16",
"@microsoft/kiota-abstractions": "1.0.0-preview.17",
"@microsoft/sp-http": "^1.15.2",
"@opentelemetry/api": "^1.2.0",
"tslib": "^2.3.1"
Expand Down
4 changes: 2 additions & 2 deletions packages/http/fetch/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/kiota-http-fetchlibrary",
"version": "1.0.0-preview.15",
"version": "1.0.0-preview.16",
"description": "Kiota request adapter implementation with fetch",
"keywords": [
"Kiota",
Expand Down Expand Up @@ -38,7 +38,7 @@
"test:cjs": "mocha 'dist/cjs/test/common/**/*.js' && mocha 'dist/cjs/test/node/**/*.js'"
},
"dependencies": {
"@microsoft/kiota-abstractions": "1.0.0-preview.16",
"@microsoft/kiota-abstractions": "1.0.0-preview.17",
"@opentelemetry/api": "^1.2.0",
"node-fetch": "^2.6.5",
"tslib": "^2.3.1"
Expand Down
13 changes: 11 additions & 2 deletions packages/http/fetch/src/fetchRequestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,16 @@ export class FetchRequestAdapter implements RequestAdapter {
});

const statusCode = response.status;
const responseHeaders: Record<string, string[]> = {};
response.headers.forEach((value, key) => {
responseHeaders[key] = value.split(",");
});
const statusCodeAsString = statusCode.toString();
if (!errorMappings || (!errorMappings[statusCodeAsString] && !(statusCode >= 400 && statusCode < 500 && errorMappings["4XX"]) && !(statusCode >= 500 && statusCode < 600 && errorMappings["5XX"]))) {
spanForAttributes.setAttribute(FetchRequestAdapter.errorMappingFoundAttributeName, false);
const error = new ApiError("the server returned an unexpected status code and no error class is registered for this code " + statusCode);
error.responseStatusCode = statusCode;
error.responseHeaders = responseHeaders;
spanForAttributes.recordException(error);
throw error;
}
Expand All @@ -327,8 +333,11 @@ export class FetchRequestAdapter implements RequestAdapter {
spanForAttributes.setAttribute(FetchRequestAdapter.errorBodyFoundAttributeName, !!error);

if (!error) error = new ApiError("unexpected error type" + typeof error) as unknown as Parsable;
spanForAttributes.recordException(error as unknown as Error);
throw error;
const errorObject = error as unknown as ApiError;
errorObject.responseStatusCode = statusCode;
errorObject.responseHeaders = responseHeaders;
spanForAttributes.recordException(errorObject);
throw errorObject;
} finally {
span.end();
}
Expand Down
24 changes: 24 additions & 0 deletions packages/http/fetch/test/common/fetchRequestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,28 @@ describe("FetchRequestAdapter.ts", () => {
});
}
});
describe("Throws API error", () => {
it("should throw API error", async () => {
const mockHttpClient = new HttpClient();
mockHttpClient.executeFetch = async (url: string, requestInit: RequestInit, requestOptions?: Record<string, RequestOption>) => {
const response = new Response("", {
status: 500,
} as ResponseInit);
response.headers.set("client-request-id", "example-guid");
return Promise.resolve(response);
};
const requestAdapter = new FetchRequestAdapter(new AnonymousAuthenticationProvider(), undefined, undefined, mockHttpClient);
const requestInformation = new RequestInformation();
requestInformation.URL = "https://www.example.com";
requestInformation.httpMethod = HttpMethod.GET;

try {
await requestAdapter.sendNoResponseContentAsync(requestInformation, undefined, undefined);
assert.fail("Should have thrown an error");
} catch (error) {
assert.equal(error.responseStatusCode, 500);
assert.equal(error.responseHeaders["client-request-id"], "example-guid");
}
});
});
});
2 changes: 1 addition & 1 deletion packages/serialization/form/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@microsoft/kiota-abstractions": "1.0.0-preview.16",
"@microsoft/kiota-abstractions": "1.0.0-preview.17",
"tslib": "^2.3.1"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion packages/serialization/json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@microsoft/kiota-abstractions": "1.0.0-preview.16",
"@microsoft/kiota-abstractions": "1.0.0-preview.17",
"tslib": "^2.3.1"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion packages/serialization/text/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"homepage": "https://github.com/microsoft/kiota-typescript#readme",
"dependencies": {
"@microsoft/kiota-abstractions": "1.0.0-preview.16",
"@microsoft/kiota-abstractions": "1.0.0-preview.17",
"tslib": "^2.3.1"
},
"publishConfig": {
Expand Down
4 changes: 2 additions & 2 deletions packages/test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
},
"dependencies": {
"@azure/identity": "^3.0.0",
"@microsoft/kiota-abstractions": "1.0.0-preview.16",
"@microsoft/kiota-abstractions": "1.0.0-preview.17",
"@microsoft/kiota-authentication-azure": "1.0.0-preview.12",
"@microsoft/kiota-http-fetchlibrary": "1.0.0-preview.15",
"@microsoft/kiota-http-fetchlibrary": "1.0.0-preview.16",
"@microsoft/kiota-serialization-form": "1.0.0-preview.6",
"@microsoft/kiota-serialization-json": "1.0.0-preview.15",
"@microsoft/kiota-serialization-text": "1.0.0-preview.14"
Expand Down