ErrorView.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import FirebaseVertexAI
  15. import SwiftUI
  16. struct ErrorView: View {
  17. var error: Error
  18. @State private var isDetailsSheetPresented = false
  19. var body: some View {
  20. HStack {
  21. Text("An error occurred.")
  22. Button(action: { isDetailsSheetPresented.toggle() }) {
  23. Image(systemName: "info.circle")
  24. }
  25. }
  26. .frame(maxWidth: .infinity, alignment: .center)
  27. .listRowSeparator(.hidden)
  28. .sheet(isPresented: $isDetailsSheetPresented) {
  29. ErrorDetailsView(error: error)
  30. }
  31. }
  32. }
  33. #Preview {
  34. NavigationView {
  35. let errorPromptBlocked = GenerateContentError.promptBlocked(
  36. response: GenerateContentResponse(candidates: [
  37. CandidateResponse(content: ModelContent(role: "model", [
  38. """
  39. A _hypothetical_ model response.
  40. Cillum ex aliqua amet aliquip labore amet eiusmod consectetur reprehenderit sit commodo.
  41. """,
  42. ]),
  43. safetyRatings: [
  44. SafetyRating(category: .dangerousContent, probability: .high),
  45. SafetyRating(category: .harassment, probability: .low),
  46. SafetyRating(category: .hateSpeech, probability: .low),
  47. SafetyRating(category: .sexuallyExplicit, probability: .low),
  48. ],
  49. finishReason: FinishReason.other,
  50. citationMetadata: nil),
  51. ])
  52. )
  53. List {
  54. MessageView(message: ChatMessage.samples[0])
  55. MessageView(message: ChatMessage.samples[1])
  56. ErrorView(error: errorPromptBlocked)
  57. }
  58. .listStyle(.plain)
  59. .navigationTitle("Chat sample")
  60. }
  61. }