I'm trying to log every possible error to a firestore document, to achieve this, I have a function which receives the error object, and saves it to the firestore document... the thing is... that this error object can be anything, it can be a single string and not an object, or it could be a complex object, appearently with functions aswell, but firestore won't accept some data types, for example if the object has an undefined property firestore will not accept it...
So I'm trying to find a way to sanitize or clean an object from which I don't know what properties could have.
Right now i'm using this:
import {db} from 'src/firebase/config'
import { addDoc, collection } from 'firebase/firestore';
import {Platform} from 'quasar'
export const saveErrorLog = async (hotel, uid, ref, error) => {
const guestRef = collection(db, 'error_logs');
const cleanErrorObject = Object.entries(error).reduce((a,[k,v]) => (v ? (a[k]=v, a) : a), {})
const logData = {
platform: Platform.is || null,
ref: ref || null,
error: { ...cleanErrorObject },
hotel: hotel || null,
uid: uid || null
}
await addDoc( guestRef, logData )
}
This works most of the time, but sometimes firebase returns the same error of unsupported data types... I think it is because some error objects have functions aswell, but I'm not sure about it.
Thanks in advance.