Skip to content

Character encoding error - SparkApiError does not exist? #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DJF3 opened this issue Nov 28, 2016 · 4 comments
Closed

Character encoding error - SparkApiError does not exist? #24

DJF3 opened this issue Nov 28, 2016 · 4 comments
Labels

Comments

@DJF3
Copy link

DJF3 commented Nov 28, 2016

When listing all the rooms of a user it stops at
room name: "Domás"
When replacing all accents in the room.title it processes the above room but stops at:
room name: "Rob Ørnebånd"

So I tried to use the example from the Readme.md file, using the try-except:

except SparkApiError as e:
    print(e)

Error message: "SparkApiError undefined" or something similar.

Question:
#1 .. How to deal with strange characters in room names?
(I just ran into room names starting with an emoticon)

#2 .. How to handle errors? (or see what happened when requesting a room list?)

Python: 3.5
Listing the rooms works perfectly when running the script from the command line instead of using CGI.

@DJF3
Copy link
Author

DJF3 commented Nov 28, 2016

BTW, fantastic library !! Job well done.

@cmlccie
Copy link
Collaborator

cmlccie commented Nov 28, 2016

@DJF3 , hmm... This is an interesting one. I pretty regularly working with room names that have special characters (mostly emoticons in my case). Can you share the full 'Exception' output when you are seeing iteration stop at these items?

You are handling errors correctly. Catching SparkApiError s should show you any errors returned by the Cisco Spark cloud. Note: The exception objects contain the full request and response objects that were involved in the API call, which you can examine from an interactive Python prompt to get additional visibility into what went wrong.

Check out:

except SparkApiError as e:
    pass

>>> e.request

>>> e.response

>>> # Especially:
>>> e.response.text

@DJF3
Copy link
Author

DJF3 commented Nov 29, 2016

Weather tried locally (macbook) or using a hosting provider:

#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
# coding=utf-8
import os
from ciscosparkapi import CiscoSparkAPI

os.environ['SPARK_ACCESS_TOKEN'] = 'xyzxyz'
api = CiscoSparkAPI()
try:
    message = api.messages.create('123098123098123098123098123', text='message_text')
    print("New message created, with ID:", message.id)
    print(message.text)
except SparkApiError as e:
    print(e)

There's no difference when using pass instead of print(e)

Generates an error message:
Traceback (most recent call last):
File "sparkapitest-message.py", line 14, in
except SparkApiError as e:
NameError: name 'SparkApiError' is not defined

This should absolutely generate an error as both the access token and the room id are wrong.
DJ

@cmlccie
Copy link
Collaborator

cmlccie commented Dec 1, 2016

...you are correct. It definitely should (and is) generating an exception. To catch a custom / package exception, you have to import that exception into your namespace. You are just missing SparkApiError from your from ciscosparkapi import ... statement.

Your code should look like this:

#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
# coding=utf-8
import os
from ciscosparkapi import CiscoSparkAPI, SparkApiError

os.environ['SPARK_ACCESS_TOKEN'] = 'xyzxyz'
api = CiscoSparkAPI()
try:
    message = api.messages.create('123098123098123098123098123', text='message_text')
    print("New message created, with ID:", message.id)
    print(message.text)
except SparkApiError as e:
    print(e)

Which runs and does indeed catch the exception(s) and print them when they are fired.

@cmlccie cmlccie closed this as completed Dec 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants