Skip to content

Commit 1596e6b

Browse files
committed
feat: enhance GLS shipment handling with improved error reporting and optional return address
1 parent 4020ef0 commit 1596e6b

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

actions/gls-action/src/helpers.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ export const getAuthToken = async (context: FunctionContext): Promise<string> =>
5454
client_secret: context.matchedConfig.findConfig("client_secret") as string,
5555
grant_type: "client_credentials",
5656
};
57+
58+
console.log(data)
5759
const url = context.matchedConfig.findConfig("auth_url") as string;
5860

5961
if (cachedToken.expiresAt > Date.now()) {
@@ -70,7 +72,7 @@ export const getAuthToken = async (context: FunctionContext): Promise<string> =>
7072
console.log(
7173
"Authentication successful, access token:",
7274
result.token_type,
73-
result.access_token.substring(0, 10) + "..."
75+
result.access_token
7476
);
7577
cachedToken = {
7678
token: result.access_token,
@@ -114,7 +116,7 @@ export const cancelShipment = async (
114116
export function getShipper(
115117
context: FunctionContext | undefined,
116118
contactID: string | undefined,
117-
shipper?: ShipperSchemaType
119+
shipper?: ShipperSchemaType | null
118120
): InternalShipper {
119121
const configShipper =
120122
(context?.matchedConfig.findConfig("default_shipper") as ShipperSchemaType) || undefined;
@@ -152,6 +154,7 @@ export function transformValidateShipmentRequestDataToInternalFormat(
152154
Shipper: getShipper(context, contactID, data.Shipment.Shipper),
153155
Service: InternalShipmentServiceSchema.parse(data.Shipment.Service),
154156
ShipmentUnit: InternalShipmentUnitSchema.parse(data.Shipment.ShipmentUnit),
157+
Return: data.Shipment.Return ? { Address: data.Shipment.Return } : undefined,
155158
},
156159
};
157160
}
@@ -169,6 +172,7 @@ export function transformShipmentRequestDataToInternalFormat(
169172
Shipper: getShipper(context, contactID, data.Shipment.Shipper),
170173
Service: InternalShipmentServiceSchema.parse(data.Shipment.Service),
171174
ShipmentUnit: InternalShipmentUnitSchema.parse(data.Shipment.ShipmentUnit),
175+
Return: data.Shipment.Return ? { Address: data.Shipment.Return } : undefined,
172176
},
173177
};
174178
}
@@ -177,6 +181,7 @@ const postShipments = async (
177181
data: ShipmentRequestData,
178182
context: FunctionContext
179183
): Promise<CreateParcelsResponse> => {
184+
console.log(context)
180185
const contactID = context.matchedConfig.findConfig("contact_id") as string;
181186
const url = context.matchedConfig.findConfig("ship_it_api_url") as string;
182187

@@ -231,13 +236,14 @@ export async function postShipmentHelper(
231236
},
232237
context
233238
);
234-
} catch (error) {
235-
if (typeof error === "string") {
236-
throw new RuntimeError("ERROR_CREATING_GLS_SHIPMENT", error);
239+
} catch (error: any) {
240+
if (error.response?.headers?.args) {
241+
const headers = error.response.headers;
242+
throw new RuntimeError("ERROR_CREATING_GLS_SHIPMENT", `GLS API error: ${headers.error}, args: ${headers.args}`);
237243
}
238-
throw new RuntimeError(
239-
"ERROR_CREATING_GLS_SHIPMENT",
240-
"An error occurred while creating the shipment."
241-
);
244+
if (error instanceof Error) {
245+
throw new RuntimeError("ERROR_CREATING_GLS_SHIPMENT", error.message);
246+
}
247+
throw new RuntimeError("ERROR_CREATING_GLS_SHIPMENT", "An error occurred while creating the GLS shipment.");
242248
}
243249
}

0 commit comments

Comments
 (0)