Skip to content

Commit

Permalink
Sessions - Replacing "Invalid ClientKey" error message with more mean…
Browse files Browse the repository at this point in the history
…ingful error (#2706) (#2710)

* removing invalid clientkey error

* changeset
  • Loading branch information
ribeiroguilherme committed Jun 12, 2024
1 parent 7b6a87f commit 5d62a4d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
22 changes: 11 additions & 11 deletions packages/lib/src/core/Services/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import AdyenCheckoutError from '../Errors/AdyenCheckoutError';
export interface HttpOptions {
accept?: string;
contentType?: string;
errorMessage?: string;
headers?;
loadingContext?: string;
method?: string;
path: string;
errorLevel?: ErrorLevel;
timeout?: number;
errorLevel?: ErrorLevel;
errorMessage?: string;
}

type ErrorLevel = 'silent' | 'info' | 'warn' | 'error' | 'fatal';

type AdyenErrorResponse = {
type AdyenApiErrorResponse = {
errorCode: string;
message: string;
errorType: string;
status: number;
};

function isAdyenErrorResponse(data: any): data is AdyenErrorResponse {
function isAdyenApiErrorResponse(data: any): data is AdyenApiErrorResponse {
return data && data.errorCode && data.errorType && data.message && data.status;
}

Expand Down Expand Up @@ -56,20 +56,20 @@ export function http<T>(options: HttpOptions, data?: any): Promise<T> {
return data;
}

if (isAdyenErrorResponse(data)) {
handleFetchError(data.message, errorLevel);
if (isAdyenApiErrorResponse(data)) {
handleFetchError(data.message, errorLevel, data);
return;
}

const errorMessage = options.errorMessage || `Service at ${url} is not available`;
handleFetchError(errorMessage, errorLevel);
handleFetchError(errorMessage, errorLevel, data);
return;
})
/**
* Catch block handles Network error, CORS error, or exception throw by the `handleFetchError`
* inside the `then` block
*/
.catch(error => {
.catch((error: unknown) => {
/**
* If error is instance of AdyenCheckoutError, which means that it was already
* handled by the `handleFetchError` on the `then` block, then we just throw it.
Expand All @@ -80,12 +80,12 @@ export function http<T>(options: HttpOptions, data?: any): Promise<T> {
}

const errorMessage = options.errorMessage || `Call to ${url} failed. Error= ${error}`;
handleFetchError(errorMessage, errorLevel);
handleFetchError(errorMessage, errorLevel, error);
})
);
}

function handleFetchError(message: string, level: ErrorLevel): void {
function handleFetchError(message: string, level: ErrorLevel, error: unknown): void {
switch (level) {
case 'silent': {
break;
Expand All @@ -97,7 +97,7 @@ function handleFetchError(message: string, level: ErrorLevel): void {
break;
}
default:
throw new AdyenCheckoutError('NETWORK_ERROR', message);
throw new AdyenCheckoutError('NETWORK_ERROR', message, { cause: error });
}
}

Expand Down
5 changes: 1 addition & 4 deletions packages/lib/src/core/Services/sessions/setup-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import Session from '../../CheckoutSession';
import { CheckoutSessionSetupResponse } from '../../CheckoutSession/types';
import { API_VERSION } from './constants';

/**
*/
function setupSession(session: Session, options): Promise<CheckoutSessionSetupResponse> {
const path = `${API_VERSION}/sessions/${session.id}/setup?clientKey=${session.clientKey}`;
const data = {
Expand All @@ -20,8 +18,7 @@ function setupSession(session: Session, options): Promise<CheckoutSessionSetupRe
{
loadingContext: session.loadingContext,
path,
errorLevel: 'fatal',
errorMessage: 'ERROR: Invalid ClientKey'
errorLevel: 'fatal'
},
data
);
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/src/pages/Dropin/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function initSession() {
actions.resolve(data);
},
onError: (error, component) => {
console.error('error', JSON.stringify(error.name), JSON.stringify(error.message), component);
console.log(error.name, error.message, error.cause);
},
// onChange: (state, component) => {
// console.log('onChange', state);
Expand Down

0 comments on commit 5d62a4d

Please sign in to comment.