Skip to content

Commit

Permalink
Bugfixing of error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jschm42 committed Nov 29, 2023
1 parent 1f31b14 commit 8b6acfe
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.talkforgeai.service.openai.dto.OpenAIImageRequest;
import com.talkforgeai.service.openai.dto.OpenAIImageResponse;
import jakarta.transaction.Transactional;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand All @@ -66,6 +67,7 @@
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import javax.imageio.ImageIO;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -385,12 +387,27 @@ public ProfileImageUploadResponse uploadImage(MultipartFile file) {
Path path = fileStorageService.getAssistantsDirectory().resolve(filename);
Files.write(path, bytes);

if (!isImageFile(path)) {
Files.delete(path);
throw new AssistentException("File is not an image.");
}

return new ProfileImageUploadResponse(filename);
} catch (IOException e) {
throw new IllegalArgumentException("Failed to upload file", e);
throw new AssistentException("Failed to upload file", e);
}
}

private boolean isImageFile(Path filePath) {
try {
BufferedImage image = ImageIO.read(filePath.toFile());
return image != null;
} catch (IOException e) {
return false;
}
}


@Transactional
public AssistantDto createAssistant(AssistantDto modifiedAssistant) {
Assistant openAIModifiedAssistant = assistantMapper.mapAssistant(modifiedAssistant);
Expand Down
23 changes: 6 additions & 17 deletions frontend/src/components/persona/PersonaTabProfile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import {defineComponent} from 'vue';
import {storeToRefs} from 'pinia';
import {usePersonaFormStore} from '@/store/persona-form-store';
import axios from 'axios';
import AssistantService from '@/service/assistant.service';
import {useChatStore} from '@/store/chat-store';
import {useAppStore} from '@/store/app-store';
Expand Down Expand Up @@ -49,23 +48,13 @@ export default defineComponent({
const selectedFile = event.target.files[0];
console.log('Selected file:', selectedFile);

const uploadedFileName = await this.uploadImage(selectedFile);
console.log('Uploaded file:', uploadedFileName.data);

this.$refs.fileInput.textContent = uploadedFileName.data.filename;

this.assistantForm.image_path = uploadedFileName.data.filename;
},
async uploadImage(file) {
const formData = new FormData();
formData.append('file', file);

try {
return await axios.post('/api/v1/persona/upload', formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
const uploadedFileName = await assistantService.uploadAssistantImage(selectedFile);
if (uploadedFileName) {
console.log('Uploaded file:', uploadedFileName.data);
this.$refs.fileInput.textContent = uploadedFileName.data.filename;
this.assistantForm.image_path = uploadedFileName.data.filename;
}
} catch (error) {
this.appStore.handleError(error);
}
Expand Down
11 changes: 11 additions & 0 deletions frontend/src/service/assistant.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ class AssistantService {
return `/api/v1/assistants/images/${imageFile}`;
}

async uploadAssistantImage(file: any) {
const formData = new FormData();
formData.append('file', file);

return await axios.post('/api/v1/assistants/images/upload', formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
}

async generateAssistantImage(prompt: string) {
return await axios.post(`/api/v1/assistants/images/generate`, {prompt});
}
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/store/app-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ export const useAppStore = defineStore('app', {
this.errors = [];
},
handleError(error: any) {
console.log('HANDLE ERROR', error);
if (error.response) {
// Request made and server responded
console.error('Error response: ', error.response.data);
console.error('Error response: ', error.response.data.message);
console.error('Error status: ', error.response.status);
console.error('Error headers: ', error.response.headers);
this.addError(error.response.data);
this.addError(error.response.data.message);
} else if (error.request) {
// The request was made but no response was received
console.error('Error request without response: ', error.request);
Expand Down

0 comments on commit 8b6acfe

Please sign in to comment.