@energica-city/shared-amplify-utils
Version:
Shared utilities for AWS Amplify projects
45 lines • 1.49 kB
JavaScript
import { logger } from '../../log';
export function ensureStructuredLogging() {
if (!logger.isStructuredLoggingEnabled()) {
logger.setStructuredLogging(true);
}
}
export function setupStructuredLoggingWith(input, buildContext, forceStructuredLogging = true, defaultContext = {}) {
if (forceStructuredLogging)
ensureStructuredLogging();
logger.setContext(buildContext(input, defaultContext));
}
export function getErrorMessage(error) {
return error instanceof Error ? error.message : String(error);
}
export function getErrorStack(error) {
return error instanceof Error ? error.stack : undefined;
}
export function parseJsonBody(body, context, scope = 'REST') {
if (!body)
return null;
try {
return JSON.parse(body);
}
catch (error) {
logger.warn(`Failed to parse ${scope} body`, {
...context,
bodyLength: body.length,
error: error instanceof Error ? error.message : String(error),
});
return null;
}
}
export function buildErrorContextWith(input, buildContext, error, additionalContext = {}) {
const base = buildContext(input, additionalContext);
return error
? { ...base, errorCode: error.code, statusCode: error.statusCode }
: base;
}
export function getModelsFromInput(input, missingMessage) {
if (!input.models) {
throw new Error(missingMessage);
}
return input.models;
}
//# sourceMappingURL=common.js.map