טיפול בשגיאות ב-Cloud Storage ב-Android
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
לפעמים דברים לא הולכים כמו שתכננתם ומתרחשת שגיאה.
אם יש ספק, כדאי לבדוק את השגיאה שהוחזרה ולראות מה כתוב בהודעת השגיאה.
בדוגמת הקוד הבאה מוצגת הטמעה של handler שגיאות מותאם אישית שבודק את קוד השגיאה ואת הודעת השגיאה שמוחזרים על ידי Cloud Storage. אפשר להוסיף את הפונקציות האלה לטיפול בשגיאות לאובייקטים שונים שמשמשים ב-API Cloud Storage (לדוגמה, UploadTask
ו-FileDownloadTask
).
Kotlin
internal inner class MyFailureListener : OnFailureListener {
override fun onFailure(exception: Exception) {
val errorCode = (exception as StorageException).errorCode
val errorMessage = exception.message
// test the errorCode and errorMessage, and handle accordingly
}
}
Java
class MyFailureListener implements OnFailureListener {
@Override
public void onFailure(@NonNull Exception exception) {
int errorCode = ((StorageException) exception).getErrorCode();
String errorMessage = exception.getMessage();
// test the errorCode and errorMessage, and handle accordingly
}
}
אם בדקתם את הודעת השגיאה ויש לכם Cloud Storage Security Rules שמאפשרים את הפעולה, אבל אתם עדיין מתקשים לתקן את השגיאה, אתם יכולים להיכנס לדף התמיכה ולעדכן אותנו איך נוכל לעזור.
טיפול בהודעות שגיאה
יכולות להיות כמה סיבות לשגיאות, כולל קובץ שלא קיים, למשתמש אין הרשאה לגשת לקובץ הרצוי או שהמשתמש ביטל את העלאת הקובץ.
כדי לאבחן את הבעיה ולטפל בשגיאה בצורה נכונה, הנה רשימה מלאה של כל השגיאות שהלקוח שלנו יציג, ואיך הן יכולות להתרחש. קודי השגיאה בטבלה הזו מוגדרים במחלקה StorageException
כקבועים של מספרים שלמים.
קוד |
סיבה |
ERROR_UNKNOWN |
אירעה שגיאה לא ידועה. |
ERROR_OBJECT_NOT_FOUND |
לא קיים אובייקט בהפניה שצוינה. |
ERROR_BUCKET_NOT_FOUND |
לא הוגדר מאגר (bucket) עבור Cloud Storage |
ERROR_PROJECT_NOT_FOUND |
לא הוגדר פרויקט עבור Cloud Storage |
ERROR_QUOTA_EXCEEDED |
חרגת מהמכסה של דלי Cloud Storage. אם אתם משתמשים בתוכנית התמחור Spark, כדאי לשדרג לתוכנית התמחור Blaze עם תשלום לפי שימוש. אם כבר נרשמתם לתוכנית התמחור Blaze, אתם יכולים לפנות לתמיכה של Firebase.
חשוב: החל מ-1 באוקטובר 2025, כדי להשתמש ב-Cloud Storage, כולל בדלי ברירת מחדל, יהיה צורך להירשם לתוכנית התמחור Blaze. |
ERROR_NOT_AUTHENTICATED |
המשתמש לא אומת. צריך לבצע אימות ולנסות שוב. |
ERROR_NOT_AUTHORIZED |
למשתמש אין הרשאה לבצע את הפעולה המבוקשת. צריך לבדוק את הכללים כדי לוודא שהם נכונים. |
ERROR_RETRY_LIMIT_EXCEEDED |
חריגה ממגבלת הזמן המקסימלית של פעולה (העלאה, הורדה, מחיקה וכו'). אפשר לנסות שוב. |
ERROR_INVALID_CHECKSUM |
הקובץ בלקוח לא תואם לסיכום הביקורת של הקובץ שהתקבל בשרת. כדאי לנסות להעלות שוב. |
ERROR_CANCELED |
המשתמש ביטל את הפעולה. |
בנוסף, ניסיון להתקשר אל getReferenceFromUrl()
עם כתובת URL לא חוקית יגרום להצגת השגיאה IllegalArgumentException
. הארגומנט של השיטה שלמעלה חייב להיות בפורמט gs://bucket/object
או https://firebasestorage.googleapis.com/v0/b/bucket/o/object?token=<TOKEN>
אלא אם צוין אחרת, התוכן של דף זה הוא ברישיון Creative Commons Attribution 4.0 ודוגמאות הקוד הן ברישיון Apache 2.0. לפרטים, ניתן לעיין במדיניות האתר Google Developers. Java הוא סימן מסחרי רשום של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-08-22 (שעון UTC).
[null,null,["עדכון אחרון: 2025-08-22 (שעון UTC)."],[],[],null,["\u003cbr /\u003e\n\nSometimes things don't go as planned and an error occurs.\n\nWhen in doubt, check the error returned and see what the error message says.\nThe following code shows a custom error handler implementation that inspects\nthe error code and error message returned by Cloud Storage. Such error\nhandlers can be added to various objects used in the Cloud Storage API (for\nexample, `UploadTask` and `FileDownloadTask`). \n\nKotlin \n\n```kotlin\ninternal inner class MyFailureListener : OnFailureListener {\n override fun onFailure(exception: Exception) {\n val errorCode = (exception as StorageException).errorCode\n val errorMessage = exception.message\n // test the errorCode and errorMessage, and handle accordingly\n }\n}https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/storage/app/src/main/java/com/google/firebase/referencecode/storage/kotlin/StorageActivity.kt#L492-L498\n```\n\nJava \n\n```java\nclass MyFailureListener implements OnFailureListener {\n @Override\n public void onFailure(@NonNull Exception exception) {\n int errorCode = ((StorageException) exception).getErrorCode();\n String errorMessage = exception.getMessage();\n // test the errorCode and errorMessage, and handle accordingly\n }\n}https://github.com/firebase/snippets-android/blob/391c1646eacf44d2aab3f76bcfa60dfc6c14acf1/storage/app/src/main/java/com/google/firebase/referencecode/storage/StorageActivity.java#L618-L625\n```\n\nIf you've checked the error message and have Cloud Storage Security Rules that allow your\naction, but are still struggling to fix the error, visit our\n[Support page](/support) and let us know how we can help.\n\nHandle Error Messages\n\nThere are a number of reasons why errors may occur, including the file\nnot existing, the user not having permission to access the desired file, or the\nuser cancelling the file upload.\n\nTo properly diagnose the issue and handle the error, here is a full list of all\nthe errors our client will raise, and how they can occur. Error codes in this\ntable are defined in the `StorageException` class as integer constants.\n\n| Code | Reason |\n|------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `ERROR_UNKNOWN` | An unknown error occurred. |\n| `ERROR_OBJECT_NOT_FOUND` | No object exists at the specified reference. |\n| `ERROR_BUCKET_NOT_FOUND` | No bucket is configured for Cloud Storage |\n| `ERROR_PROJECT_NOT_FOUND` | No project is configured for Cloud Storage |\n| `ERROR_QUOTA_EXCEEDED` | Quota on your Cloud Storage bucket has been exceeded. If you're on the Spark pricing plan, consider upgrading to the [pay-as-you-go Blaze pricing plan](/pricing). If you're already on the Blaze pricing plan, reach out to Firebase Support. **Important** : Starting October 1, 2025, the [Blaze pricing plan will be *required* to use Cloud Storage](/docs/storage/faqs-storage-changes-announced-sept-2024), even default buckets. |\n| `ERROR_NOT_AUTHENTICATED` | User is unauthenticated, please authenticate and try again. |\n| `ERROR_NOT_AUTHORIZED` | User is not authorized to perform the requested action, check your rules to ensure they are correct. |\n| `ERROR_RETRY_LIMIT_EXCEEDED` | The maximum time limit on an operation (upload, download, delete, etc.) has been excceded. Try again. |\n| `ERROR_INVALID_CHECKSUM` | File on the client does not match the checksum of the file received by the server. Try uploading again. |\n| `ERROR_CANCELED` | User canceled the operation. |\n\nAdditionally, attempting to call `getReferenceFromUrl()` with an invalid URL\nwill result in an `IllegalArgumentException` from being thrown. The argument to\nthe above method must be of the form `gs://bucket/object` or\n`https://firebasestorage.googleapis.com/v0/b/bucket/o/object?token=\u003cTOKEN\u003e`"]]