npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

firebase-error-translator

v2.0.2

Published

Helps to handle the firebase errors based on their codes, you can translate the error to any of the available languages. (Now only works with auth errors)

Downloads

36

Readme

Helps to handle the firebase errors based on their codes, you can translate the error to any of the available languages, now it also adds a translation to the client.


Build Status Libraries.io dependency status for latest release npm npm GitHub Repo stars

Services

For now, the version only supports translations for:

  • Auth: ✅
  • Storage: ✅
  • Firestore: ❌ (in the future, when I could find a documentation for the firestore errors)

Because new features are added, now it can only translate to 2 langs: English 'en', and Spanish 'es'; this is because that are the languages that I know how to write some phrases that explain the errors.

Basic Use

Note: This syntax is using module imports, but if you want to use commonjs modules, you can use them.

To use the function, import the translate function, and a TranslateDictionary, deppending on the service of the error and the language you want to translate; in this case the service of the possible error is Auth (firebase/auth), and the language I want to translate is spanish (es)

import { translate, AuthEs } from 'firebase-error-translator'

...

const registerUser = async (email, password) => {
    try{
        await signInWithEmailAndPassword(email, password)
    }
    catch(err){
        handleError(translate(err, AuthEs))
    }
}

...

translate will search the FirebaseError code in the given TranslateDictionary, and will return a TranslateObject, which has the properties toDev with a explained translation, and toClient, for ocassions where you want to show the error to the user (don't worry, not all errors display a detailed message).

...
// Lets asume that the error code is 'auth/user-not-found'
const translation = translate(err, AuthEs)

// Show the explained error to the developer
console.error(translation.toDev) // No existe ningún registro de usuario que corresponda al identificador proporcionado.

// Show a little hint of the error to the client
todoShowError(translation.toClient) // Usuario no encontrado, por favor revise su información.

In some cases, translation.toClient will return a detailled message like 'The provided email is taken by another user, please write another one.', and in another ocassions, it will only return a message like 'Server Error.', it deppends of the error.

...
// Lets asume now that the error code is 'auth/claims-too-large'
const translation = translate(err, AuthEn) // Note: Now we are using AuthEn instead of AuthEs, to get english translations

// Show the explained error to the developer
console.error(translation.toDev) // The claims payload provided to setCustomUserClaims() exceeds the maximum allowed size of 1000 bytes.

// Show a little hint of the error to the client
todoShowError(translation.toClient) // Server Error.

Multiple Dictionaries

If you don't know exactly what type of error are you getting (maybe in some function that can fail getting your auth and a storage), you can provide many TranslationDictionaries as you want.

...
const veryConfusingFunction = async () => {
    try{
        const userPhoto = await getUserPhoto() // Here can fail the auth service
        await uploadPhoto(userPhoto) // Here can fail the storage service
    }
    catch(err){
        // We provide 2 dictionaries because we don't know
        // in which function will fail
        handleError(translate(err, AuthES, StorageES))
    }
}

Custom Errors and Not Error Handling

If you want to force an error, you can make it with fail(), you only need to write the language of the error, the developer message and the client message.

import { fail, translate, AuthEN } from 'firebase-error-translator'

...

const loadForm = () => {
    try{
        if(email === ''){
            fail('en', 'The user didnt write its email, what a dumb.', 'Email is required, please write it:).')
        }
    }
    catch(err){
        const translation = translate(err, AuthEN)

        console.error(translation.toDev) // The user didnt write its email, what a dumb.
        todoShowError(translation.toClient) // Email is required, please write it:).
    }
}

You can also use translate with normal errors and values with toString() methods.

...

const failWithError = () => {
    try{
        throw new Error('My function failed.')
    }
    catch(err){
        console.log(translate(err, StorageES)) // We must provide at least one dictionary, this is to detect the language, the storage dictionaries are the shortest so we gonna use them

        /*
         * {
              toClient: 'Error de Servidor.',
              toDev: 'My function failed.',
         * }
         */
    } 
}

const failWithToString = () => {
    try{
        throw new Date()
    }
    catch(err){
        console.log(translate(err, StorageES)) // We must provide at least one dictionary, this is to detect the language, the storage dictionaries are the shortest so we gonna use them

        /*
         * {
              toClient: 'Error de Servidor.',
              toDev: '2022-07-29T02:51:51.246Z',
         * }
         */
    } 
}

TranslationDictionaries

The translation dictionaries are objects that start with the Firebase service of the codes that it contains (Auth or Storage right now), following by the language code based in the ISO 639-1 rules.