xDimGG / node-steamapi

An object-oriented Steam API wrapper for Node.js developers.
https://www.npmjs.com/package/steamapi
183 stars 44 forks source link

Don't Promise.reject causing server error 500. console.error instead #35

Closed fungilation closed 1 year ago

fungilation commented 2 years ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch steamapi@2.2.0 for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/steamapi/src/SteamAPI.js b/node_modules/steamapi/src/SteamAPI.js
index d479a8b..f291dc4 100644
--- a/node_modules/steamapi/src/SteamAPI.js
+++ b/node_modules/steamapi/src/SteamAPI.js
@@ -291,7 +291,7 @@ class SteamAPI {
     */
    getUserBans(id) {
        const arr = Array.isArray(id);
-       if ((arr && id.some(i => !reID.test(i))) || (!arr && !reID.test(id))) return Promise.reject(new TypeError('Invalid/no id provided'));
+       if ((arr && id.some(i => !reID.test(i))) || (!arr && !reID.test(id))) return console.error('SteamAPI: Invalid/no id provided');

        return this
            .get(`/ISteamUser/GetPlayerBans/v1?steamids=${id}`)
@@ -299,7 +299,7 @@ class SteamAPI {
                ? arr
                    ? json.players.map(player => new PlayerBans(player))
                    : new PlayerBans(json.players[0])
-               : Promise.reject(new Error('No players found'))
+               : console.error('SteamAPI: No players found')
            );
    }

@@ -351,11 +351,11 @@ class SteamAPI {
     * @returns {Promise<Game[]>} Owned games
     */
    getUserOwnedGames(id) {
-       if (!reID.test(id)) return Promise.reject(new TypeError('Invalid/no id provided'));
+       if (!reID.test(id)) return console.error('SteamAPI: Invalid/no id provided');

        return this
            .get(`/IPlayerService/GetOwnedGames/v1?steamid=${id}&include_appinfo=1`)
-           .then(json => json.response.games ? json.response.games.map(game => new Game(game)) : Promise.reject(new Error('No games found')));
+           .then(json => json.response.games ? json.response.games.map(game => new Game(game)) : console.error('SteamAPI: No games found'));
    }

    /**

This issue body was partially generated by patch-package.

xDimGG commented 2 years ago

I'm confused as to how this is a fix. This just makes it harder to handle errors. Perhaps you should make your code handle promise rejections instead?