Giter Club home page Giter Club logo

Comments (9)

cnguy avatar cnguy commented on July 24, 2024 1

If you place it inside the try block, the whole try statement will run from start-to-finish unless an error occurs. So you'll be able to catch 404's, 500's, etc of both Summoner and CurrentGame. Not sure about the scoping part, but IMO if the logic is "combined", then put it all in one try block.

I would put it in there personally, unless you want specific error-handling on a case-by-case basis.

from kayn.

cnguy avatar cnguy commented on July 24, 2024 1

Ohhh, I haven't been reading carefully. you can try this:

const nombre = args.slice([1]).join(' ');
const nombreUrl = nombre.replace(/\s/g, "%20").toLowerCase();
let summoner = null;
try {
  summoner = await kayn.Summoner.by.name(nombre);
} catch (ex) {
  if (ex.statusCode == 404) game = null;
  else throw new Error("completely broken");
}
let game = null;
try {
  game = await kayn.CurrentGame.by.summonerID(summoner.id) // awaiting doesn't solve it
} catch (ex) {
  if (ex.statusCode == 404) game = null;
  else throw new Error("completely broken");
}
console.log(summoner, game)

Basically, if there's a 404, just reassign null, otherwise if it's some other error that mean something really bad happened (assuming retries are on), so just throw.

from kayn.

cnguy avatar cnguy commented on July 24, 2024

You forgot to await

kayn.CurrentGame.by.summonerID(summoner.id)

as well. All of kayn's requests return promises that must be awaited.

from kayn.

viktordimov avatar viktordimov commented on July 24, 2024

Same result.

(node:8832) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'then' of undefined
    at Request.catch.callback [as catch] (C:\Users\viks\Desktop\basil\node_modules\kayn\dist\lib\RequestClient\Request.js:67:49)
    at Object.execute (C:\Users\viks\Desktop\basil\comandos\riotAPI.js:46:71)
    at Client.client.on (C:\Users\viks\Desktop\basil\basil.js:55:11)
    at Client.emit (events.js:203:15)
    at MessageCreateHandler.handle (C:\Users\viks\Desktop\basil\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\viks\Desktop\basil\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65)
    at WebSocketConnection.onPacket (C:\Users\viks\Desktop\basil\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\viks\Desktop\basil\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\viks\Desktop\basil\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:198:13)
    at Receiver.receiverOnMessage (C:\Users\viks\Desktop\basil\node_modules\ws\lib\websocket.js:789:20)
    at Receiver.emit (events.js:198:13)
    at Receiver.dataMessage (C:\Users\viks\Desktop\basil\node_modules\ws\lib\receiver.js:422:14)
    at Receiver.getData (C:\Users\viks\Desktop\basil\node_modules\ws\lib\receiver.js:352:17)
    at Receiver.startLoop (C:\Users\viks\Desktop\basil\node_modules\ws\lib\receiver.js:138:22)
    at Receiver._write (C:\Users\viks\Desktop\basil\node_modules\ws\lib\receiver.js:74:10)
    at doWrite (_stream_writable.js:415:12)
    at writeOrBuffer (_stream_writable.js:399:5)
    at Receiver.Writable.write (_stream_writable.js:299:11)
    at TLSSocket.socketOnData (C:\Users\viks\Desktop\basil\node_modules\ws\lib\websocket.js:864:35)
    at TLSSocket.emit (events.js:198:13)
    at addChunk (_stream_readable.js:288:12)

from kayn.

cnguy avatar cnguy commented on July 24, 2024

Hmm, I have a feeling that

const summoner = await kayn.Summoner.by.name(nombre) // this line
let game = await kayn.CurrentGame.by.summonerID(summoner.id)

The commented line may be returning an error.

You can try this:

try {
  const summoner = await kayn.Summoner.by.name(nombre)
  let game = await kayn.CurrentGame.by.summonerID(summoner.id) // awaiting doesn't solve it
  console.log(typeof(game));
} catch (error) {
  console.log(error)
}

to see what the error is.

from kayn.

viktordimov avatar viktordimov commented on July 24, 2024
{ statusCode: 404,
  url:
   'https://euw1.api.riotgames.com/lol/spectator/v4/active-games/by-summoner/omitted-the-ID-on-github-comment',
  error:
   { name: 'StatusCodeError',

Alright! This worked just fine, now I can reasign with !game.statusCode === 200. Must I place kayn.CurrentGame inside the Try block, or any call that may return a 404? does it scope it?
Thanks for your help, this saved my day!

from kayn.

viktordimov avatar viktordimov commented on July 24, 2024

Looks like there's no way to redefine the variable if there is no current game for the summoner, or at least that I'm capable of. Thanks for your help anyway! :)

from kayn.

viktordimov avatar viktordimov commented on July 24, 2024

O M G

So I can actually reasign if the variable is declared outside the try{} catch {} block in the first place?!
Yes! This returns the current game object if there is one and allows me to reasign game to "No current games" if it's value is null due to ex.statusCode == 404. No bugs here, just an ES6 noob.
Thank you

from kayn.

cnguy avatar cnguy commented on July 24, 2024

it's okay haha, good luck! I didn't read the question properly so my first answer or 2 were not actually answering the question properly

from kayn.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.