Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sessions - Replacing "Invalid ClientKey" error message with more meaningful error #2706

Merged
merged 2 commits into from
Jun 11, 2024
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
5 changes: 5 additions & 0 deletions .changeset/purple-kids-ring.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@adyen/adyen-web": patch
---

For sessions /setup call, we are passing the full error object instead of hardcoded 'ERROR: Invalid ClientKey' message
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 @@ -5,25 +5,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 @@ -57,20 +57,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 @@ -81,12 +81,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 @@ -98,7 +98,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 '../../../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 @@ -28,7 +28,7 @@ export async function initSession() {
console.info(result, component);
},
onError: (error, component) => {
console.info(JSON.stringify(error), component);
console.log(error.name, error.message, error.cause);
},
// onChange: (state, component) => {
// console.log('onChange', state);
Expand Down
Loading