Skip to content

Commit

Permalink
fix(Designer): Create connection now uses testRequests when availab…
Browse files Browse the repository at this point in the history
…le (#4419)

* Modified test connection code to prioritize testRequests over testLinks

* Combined types
  • Loading branch information
rllyy97 committed Mar 25, 2024
1 parent 0db89fe commit 45928d0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
6 changes: 4 additions & 2 deletions libs/logic-apps-shared/src/utils/src/lib/models/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ export interface ConnectionProperties {
overallStatus: string;
parameterValueType?: string;
statuses: ConnectionStatus[];
testLinks?: TestLink[];
testLinks?: TestConnectionObject[];
testRequests?: TestConnectionObject[];
accountName?: string;
api: Api;
connectionRuntimeUrl?: string;
}

export type Connection = ArmResource<ConnectionProperties>;

export interface TestLink {
interface TestConnectionObject {
body?: string;
method: string;
requestUri: string;
}
41 changes: 33 additions & 8 deletions libs/services/designer-client-services/src/lib/base/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,24 +205,24 @@ export abstract class BaseConnectionService implements IConnectionService {

protected async testConnection(connection: Connection): Promise<void> {
let response: HttpResponse<any> | undefined = undefined;
const testLinks = connection.properties?.testLinks;
if (!testLinks || testLinks.length === 0) return;
response = await this.requestTestConnection(connection);
if (response) this.handleTestConnectionResponse(response);
}

protected async requestTestConnection(connection: Connection): Promise<HttpResponse<any> | undefined> {
const testLinks = connection.properties?.testLinks;
if (!testLinks || testLinks.length === 0) return undefined;
const testRequests = connection.properties?.testRequests;
const { httpClient } = this.options;
const { method: httpMethod, requestUri: uri } = testLinks[0];
const { method: httpMethod, requestUri: uri, body } = testRequests?.[0] ?? testLinks?.[0] ?? {};
if (!httpMethod || !uri) return;
const method = httpMethod.toUpperCase() as HTTP_METHODS;

try {
let response: HttpResponse<any> | undefined = undefined;
const requestOptions: HttpRequestOptions<any> = {
headers: { noBatch: 'true' }, // Some requests fail specifically when run through batch
uri,
...(body ? { content: body } : {}),
};
if (equals(method, HTTP_METHODS.GET)) response = await httpClient.get<any>(requestOptions);
else if (equals(method, HTTP_METHODS.POST)) response = await httpClient.post<any, any>(requestOptions);
Expand All @@ -236,17 +236,42 @@ export abstract class BaseConnectionService implements IConnectionService {

protected handleTestConnectionResponse(response?: HttpResponse<any>): void {
if (!response) return;
if (response?.status) return this.handleTestLinkResponse(response);
if ((response as any)?.response) return this.handleTestRequestResponse((response as any)?.response);
}

private handleTestLinkResponse(response: HttpResponse<any>): void {
const defaultErrorResponse = 'Please check your account info and/or permissions and try again.';
const status = response?.status;
if (status >= 400 && status < 500 && status !== 429) {
let errorMessage = defaultErrorResponse;
const body = response?.body;
if (body && typeof body === 'string') errorMessage = this.tryParseErrorMessage(JSON.parse(body), defaultErrorResponse);
throw new UserException(UserErrorCode.TEST_CONNECTION_FAILED, errorMessage);
}
}

private handleTestRequestResponse(response: any): void {
const defaultErrorResponse = 'Please check your account info and/or permissions and try again.';
if (response.status >= 400 && response.status < 500 && response.status !== 429) {
const statusCode = response?.statusCode;
if (statusCode !== 'OK') {
let errorMessage = defaultErrorResponse;
if (response.body && typeof response.body === 'string')
errorMessage = this.tryParseErrorMessage(JSON.parse(response.body), defaultErrorResponse);
const body = response?.body;
if (body) errorMessage = this.tryParseErrorMessage(body, defaultErrorResponse);
throw new UserException(UserErrorCode.TEST_CONNECTION_FAILED, errorMessage);
}
}

protected tryParseErrorMessage(error: any, defaultErrorMessage?: string): string {
return error?.message ?? error?.Message ?? error?.error?.message ?? error?.responseText ?? defaultErrorMessage ?? 'Unknown error';
return (
error?.message ??
error?.Message ??
error?.error?.message ??
error?.responseText ??
error?.errors?.map((e: any) => e?.message ?? e)?.join(', ') ??
defaultErrorMessage ??
'Unknown error'
);
}

protected async getConnectionsForConnector(connectorId: string): Promise<Connection[]> {
Expand Down

0 comments on commit 45928d0

Please sign in to comment.