Files
ohm_streaming/static/js/auth-utils.js
root d4d8d8a3b6
CI / Test (Python 3.11) (push) Has been cancelled
CI / Test (Python 3.12) (push) Has been cancelled
CI / Lint (push) Has been cancelled
CI / Type Check (push) Has been cancelled
CI / Summary (push) Has been cancelled
refactor: migrate main.py to modular routers and add project roadmap
- Migrated monolithic main.py to feature-scoped routers in app/routers/
- Added GEMINI.md for project context and AI instructional guidelines
- Updated README.md with a comprehensive modernization plan (SQL migration, robust scraping DSL, frontend modernization)
- Improved authentication with cookie support and modular JS
- Updated test suite and documentation
2026-03-24 10:12:04 +00:00

106 lines
3.1 KiB
JavaScript

/**
* Auth utilities - safe JSON parsing and error display
* Following the pattern from static/js/watchlist.js (global exports)
*/
// API base URL - use relative path for same-origin
const API_BASE = '/api';
/**
* Safely parse JSON string with fallback
* @param {string} text - The JSON string to parse
* @param {*} fallback - The fallback value if parsing fails (default: null)
* @returns {*} Parsed object or fallback value
*/
function safeJsonParse(text, fallback = null) {
try {
if (text === undefined || text === null || text === '') {
return fallback;
}
return JSON.parse(text);
} catch (error) {
console.error('JSON parse error:', error.message);
return fallback;
}
}
/**
* Display error message in the specified element
* Handles string, object, and array errors properly
* @param {string} elementId - The ID of the element to display error in
* @param {*} error - The error (string, object, or array)
* @param {string} defaultMessage - Default message if error is invalid
*/
function displayError(elementId, error, defaultMessage = 'Une erreur est survenue') {
const errorDiv = document.getElementById(elementId);
if (!errorDiv) {
console.error('Error element not found:', elementId);
return;
}
let message = defaultMessage;
if (error === null || error === undefined) {
message = defaultMessage;
} else if (typeof error === 'string') {
message = error;
} else if (typeof error === 'object') {
// Handle array errors
if (Array.isArray(error)) {
message = error.join('\n');
}
// Handle FastAPI HTTPException detail (can be string or object)
else if (error.detail) {
if (typeof error.detail === 'string') {
message = error.detail;
} else if (typeof error.detail === 'object' && error.detail.msg) {
message = error.detail.msg;
} else {
// Stringify the object to avoid "[object Object]"
message = JSON.stringify(error.detail);
}
}
// Handle generic object
else {
message = JSON.stringify(error);
}
}
errorDiv.textContent = message;
errorDiv.classList.add('show');
// Hide success message if visible
const successDiv = document.getElementById(elementId.replace('Error', 'Success'));
if (successDiv) {
successDiv.classList.remove('show');
}
}
/**
* Display success message in the specified element
* @param {string} elementId - The ID of the element to display success in
* @param {string} message - The success message
*/
function displaySuccess(elementId, message) {
const successDiv = document.getElementById(elementId);
if (!successDiv) {
console.error('Success element not found:', elementId);
return;
}
successDiv.textContent = message;
successDiv.classList.add('show');
// Hide error message if visible
const errorDiv = document.getElementById(elementId.replace('Success', 'Error'));
if (errorDiv) {
errorDiv.classList.remove('show');
}
}
// Export globally (following watchlist.js pattern)
window.safeJsonParse = safeJsonParse;
window.displayError = displayError;
window.displaySuccess = displaySuccess;
window.API_BASE = API_BASE;