Skip to content

Commit

Permalink
Merge pull request #338 from bsinno/feature/#331_return_error_log_whe…
Browse files Browse the repository at this point in the history
…n_generation_fails

Return error log when code generation fails; fixes #331;
  • Loading branch information
aedelmann authored Jul 18, 2016
2 parents 90074c8 + 61ae0ea commit 7e08240
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.vorto.codegen.api.Generated;
import org.eclipse.vorto.codegen.api.GenerationResultZip;
import org.eclipse.vorto.codegen.api.IGenerationResult;
import org.eclipse.vorto.codegen.api.IVortoCodeGenerator;
import org.eclipse.vorto.codegen.api.mapping.InvocationContext;
Expand Down Expand Up @@ -48,17 +50,17 @@ protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask(
"Invoking generator [" + codeGenerator.getServiceKey() + "]",
1);
IGenerationResult generatedResult = null;
try {
IGenerationResult generatedResult = ((IVortoCodeGenerator) codeGenerator).generate(informationModel,invocationContext);
CodeGenerationHelper.createEclipseProject(ModelIdFactory.newInstance(informationModel), codeGenerator.getServiceKey(), generatedResult);
monitor.worked(1);
if (monitor.isCanceled()) {
monitor.done();
return Status.CANCEL_STATUS;
}

generatedResult = ((IVortoCodeGenerator) codeGenerator).generate(informationModel,invocationContext);
} catch (Exception e) {
e.printStackTrace();
MessageDisplayFactory.getMessageDisplay().displayError(e);
return Status.CANCEL_STATUS;
}
CodeGenerationHelper.createEclipseProject(ModelIdFactory.newInstance(informationModel), codeGenerator.getServiceKey(), generatedResult);
monitor.worked(1);
if (monitor.isCanceled()) {
monitor.done();
return Status.CANCEL_STATUS;
}
monitor.done();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface IVortoCodeGenerator {
* @param context generation invocation context, e.g. mapping rules or invocation parameters
* @return
*/
IGenerationResult generate(InformationModel model, InvocationContext context);
IGenerationResult generate(InformationModel model, InvocationContext context) throws Exception;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import javax.annotation.PostConstruct;

import org.apache.commons.io.IOUtils;
import org.eclipse.vorto.codegen.api.Generated;
import org.eclipse.vorto.codegen.api.GenerationResultZip;
import org.eclipse.vorto.codegen.api.GeneratorServiceInfo;
import org.eclipse.vorto.codegen.api.IGenerationResult;
import org.eclipse.vorto.codegen.api.IVortoCodeGenerator;
Expand Down Expand Up @@ -92,15 +94,23 @@ public ResponseEntity<InputStreamResource> generate(@PathVariable String namespa

InformationModel infomodel = extractor.extract(new ModelId(ModelType.InformationModel, name, namespace, version));

IGenerationResult result = vortoGenerator.generate(infomodel, resolveMappingContext(infomodel, vortoGenerator.getServiceKey()));

IGenerationResult result = null;
try {
result = vortoGenerator.generate(infomodel, resolveMappingContext(infomodel, vortoGenerator.getServiceKey()));
} catch (Exception e) {
GenerationResultZip output = new GenerationResultZip(infomodel,vortoGenerator.getServiceKey());
Generated generated = new Generated("generation_error.log", "/generated", e.getMessage());
output.write(generated);
result = output;
}

return ResponseEntity.ok().contentLength(result.getContent().length)
.header("content-disposition", "attachment; filename = " + result.getFileName())
.contentType(MediaType.parseMediaType(result.getMediatype()))
.body(new InputStreamResource(new ByteArrayInputStream(result.getContent())));

}


private InvocationContext resolveMappingContext(InformationModel model, String targetPlatform) {
byte[] mappingResources = downloadMappingModel(model, targetPlatform);
return new MappingZipFileExtractor(mappingResources).extract();
Expand Down

0 comments on commit 7e08240

Please sign in to comment.