60 lines
No EOL
2.4 KiB
JavaScript
60 lines
No EOL
2.4 KiB
JavaScript
"use strict";
|
|
/**
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the MIT License.
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.AuthenticationError = void 0;
|
|
const botframework_schema_1 = require("botframework-schema");
|
|
/**
|
|
* General `AuthenticationError` class to represent an Authentication error with a Code Status.
|
|
*/
|
|
class AuthenticationError extends Error {
|
|
/**
|
|
* Initializes a new instance of the [AuthenticationError](xref:botframework-connector.AuthenticationError) class.
|
|
*
|
|
* @param message The Error message.
|
|
* @param statusCode The `StatusCode` number to use.
|
|
*/
|
|
constructor(message, statusCode) {
|
|
super(message);
|
|
this.statusCode = statusCode;
|
|
}
|
|
/**
|
|
* Corroborates that the error is of type [IStatusCodeError](xref:botframework-schema.IStatusCodeError).
|
|
*
|
|
* @param err The error to validate.
|
|
* @returns If `err` is an [IStatusCodeError](xref:botframework-schema.IStatusCodeError), the result is true; otherwise false.
|
|
*/
|
|
static isStatusCodeError(err) {
|
|
return !!(err && typeof err.statusCode === 'number');
|
|
}
|
|
/**
|
|
* Used to determine a status code from the error message for non-`IStatusCodeError`'s.
|
|
*
|
|
* @param err The error thrown, used to determine an appropriate status code.
|
|
* @returns The error message to be sent as a response.
|
|
*/
|
|
static determineStatusCodeAndBuildMessage(err) {
|
|
const errMessage = err && err.message ? err.message : 'Internal Server Error';
|
|
const code = AuthenticationError.determineStatusCode(errMessage);
|
|
const connectionHeader = "Connection: 'close'\r\n";
|
|
return `HTTP/1.1 ${code} ${botframework_schema_1.StatusCodes[code]}\r\n${errMessage}\r\n${connectionHeader}\r\n`;
|
|
}
|
|
/**
|
|
* @private
|
|
*/
|
|
static determineStatusCode(message) {
|
|
if (typeof message === 'string') {
|
|
if (message.toLowerCase().startsWith('unauthorized')) {
|
|
return botframework_schema_1.StatusCodes.UNAUTHORIZED;
|
|
}
|
|
else if (message.toLowerCase().startsWith("'authheader'")) {
|
|
return botframework_schema_1.StatusCodes.BAD_REQUEST;
|
|
}
|
|
}
|
|
return botframework_schema_1.StatusCodes.INTERNAL_SERVER_ERROR;
|
|
}
|
|
}
|
|
exports.AuthenticationError = AuthenticationError;
|
|
//# sourceMappingURL=authenticationError.js.map
|